2016-04-25 43 views
9

我遇到的问题只会影响IE和Edge(在IE9-11 & Edge 12-13上测试过)。我使用Snap.svg为​​SVG笔画偏移设置了动画效果,似乎可以正常运行,但在某些时候,笔画看上去会“超出界限”并消失。这很奇怪,因为viewBox被设置为与线条完全吻合(它直接从Illustrator中导出,并带有适合整条线的美工板)。SVG动画在IE和Edge上出现“越界”

您可以在这里看到这些内容起作用:http://codepen.io/ndimatteo/full/zqLBVW/

// SSSSSNAKE 
 
var snake = Snap('#snake-preview svg'); 
 
var bodyPath = snake.select('#snake-body'); 
 

 
var bodyPathBreakfast = snake.select('#snake-body-breakfast'); 
 
var bodyPathLunch = snake.select('#snake-body-lunch'); 
 
var bodyPathDinner = snake.select('#snake-body-dinner'); 
 

 
var headPath = snake.select('#snake-head'); 
 

 
// Draw Path 
 
var bodyLength = bodyPath.getTotalLength(); 
 
var headLength = headPath.getTotalLength(); 
 

 
function snakeAnim() { 
 

 
    //reset food 
 
    setTimeout(function() { 
 
    $('#snake-body-dinner').hide(); 
 
    snake.animate({ opacity: 1}, 200); 
 
    $('#food-1').show(); 
 
    }, 1000); 
 

 
    // breakfast 
 
    setTimeout(function() { 
 
    $('#food-1').hide(); 
 
    $('#food-2, #snake-body-breakfast').show(); 
 
    }, 2000); 
 
    
 
    // lunch 
 
    setTimeout(function() { 
 
    $('#food-2, #snake-body-breakfast').hide(); 
 
    $('#food-3, #snake-body-lunch').show(); 
 
    }, 7000); 
 

 
    // dinner 
 
    setTimeout(function() { 
 
    $('#food-3, #snake-body-lunch').hide(); 
 
    $('#snake-body-dinner').show(); 
 
    }, 12800); 
 

 
    // death 
 
    setTimeout(function() { 
 
    snake.animate({ opacity: 0}, 200, snakeAnim); 
 
    }, 14800); 
 

 
    // give it a head 
 
    headPath.attr({ 
 
    "stroke-dasharray": 1 + ' ' + headLength, 
 
    "stroke-dashoffset": 0 
 
    }).animate({ 
 
    "stroke-dashoffset": -headLength 
 
    }, 15000, mina.linear); 
 

 
    // breakfast bite 
 
    bodyPathBreakfast.attr({ 
 
    "stroke-dasharray": 250 + ' ' + (bodyLength-250), 
 
    "stroke-dashoffset": 250 
 
    }).animate({ 
 
    "stroke-dashoffset": -bodyLength+250 
 
    }, 15000, mina.linear); 
 

 
    // lunch bite 
 
    bodyPathLunch.attr({ 
 
    "stroke-dasharray": 300 + ' ' + (bodyLength-300), 
 
    "stroke-dashoffset": 300 
 
    }).animate({ 
 
    "stroke-dashoffset": -bodyLength+300 
 
    }, 15000, mina.linear); 
 

 
    // dinner bite 
 
    bodyPathDinner.attr({ 
 
    "stroke-dasharray": 350 + ' ' + (bodyLength-350), 
 
    "stroke-dashoffset": 350 
 
    }).animate({ 
 
    "stroke-dashoffset": -bodyLength+350 
 
    }, 15000, mina.linear); 
 
    
 
    // hungry snake 
 
    bodyPath.attr({ 
 
    "stroke-dasharray": 200 + ' ' + (bodyLength-200), 
 
    "stroke-dashoffset": 200 
 
    }).animate({ 
 
    "stroke-dashoffset": -bodyLength+200 
 
    }, 15000, mina.linear); 
 
} 
 

 
snakeAnim();
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
body { 
 
    background: #fcd835; 
 
    text-align: center; 
 
    height: 100%; 
 
} 
 

 
#snake-preview { 
 
    position: relative; 
 
    margin: 0 auto; 
 
    width: 100%; 
 
    height: 0; 
 
    padding-top: 50%; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
} 
 
#snake-preview svg { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    opacity: 0; 
 
} 
 
#snake-preview svg #food-1, 
 
#snake-preview svg #food-2, 
 
#snake-preview svg #food-3, 
 
#snake-preview svg #snake-body-breakfast, 
 
#snake-preview svg #snake-body-lunch, 
 
#snake-preview svg #snake-body-dinner { 
 
    display: none; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.4.1/snap.svg.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="snake-preview"> 
 
    <svg viewBox="0 0 800 400"><rect x="313.91" y="230.68" width="14.14" height="14.14" transform="translate(-74.1 296.61) rotate(-45)" fill="#304fff" id="food-1"/><rect x="547.1" y="116.93" width="14.14" height="14.14" transform="translate(74.63 428.17) rotate(-45)" fill="#304fff" id="food-2"/><rect x="423.93" y="262.93" width="14.14" height="14.14" transform="translate(-64.68 383.84) rotate(-45)" fill="#304fff" id="food-3"/><path d="M0 164.25 320.98 164.25 320.98 237.75 424.16 237.75 424.16 191.25 494.98 191.25 494.98 68.26 524.52 68.26 524.52 291.74 647.98 291.74 647.98 124 386 124 386 322 242 322 242 79 431 79 431 322 800 322" fill="none" stroke="#ff1644" stroke-miterlimit="10" stroke-width="20" id="snake-body"/><path d="M0 164.25 320.98 164.25 320.98 237.75 424.16 237.75 424.16 191.25 494.98 191.25 494.98 68.26 524.52 68.26 524.52 291.74 647.98 291.74 647.98 124 386 124 386 322 242 322 242 79 431 79 431 322 800 322" fill="none" stroke="#ff1644" stroke-miterlimit="10" stroke-width="20" id="snake-body-breakfast"/><path d="M0 164.25 320.98 164.25 320.98 237.75 424.16 237.75 424.16 191.25 494.98 191.25 494.98 68.26 524.52 68.26 524.52 291.74 647.98 291.74 647.98 124 386 124 386 322 242 322 242 79 431 79 431 322 800 322" fill="none" stroke="#ff1644" stroke-miterlimit="10" stroke-width="20" id="snake-body-lunch"/><path d="M0 164.25 320.98 164.25 320.98 237.75 424.16 237.75 424.16 191.25 494.98 191.25 494.98 68.26 524.52 68.26 524.52 291.74 647.98 291.74 647.98 124 386 124 386 322 242 322 242 79 431 79 431 322 800 322" fill="none" stroke="#ff1644" stroke-miterlimit="10" stroke-width="20" id="snake-body-dinner"/><path d="M0 164.25 320.98 164.25 320.98 237.75 424.16 237.75 424.16 191.25 494.98 191.25 494.98 68.26 524.52 68.26 524.52 291.74 647.98 291.74 647.98 124 386 124 386 322 242 322 242 79 431 79 431 322 800 322" fill="none" stroke="#ffffff" stroke-miterlimit="10" stroke-width="20" stroke-dasharray="1 2000" stroke-linecap="round" id="snake-head"/></svg> 
 
    </div>

+0

在技术笔记上,不要浪费时间在IE9和IE10上 - 微软在今年1月12日停止使用它们,除了那些只能使用IE9的Vista SP2用户,并且可能仍然在MS的扩展支持范围内;但只有当他们是一个企业,而不是在家的普通人(我确信你不是针对这里的企业)。新的MS政策是“我们只支持IE的最新版本”,这就是IE11,一旦Edge登陆,IE11也将失去支持。 –

+0

谢谢Mike,我完全同意。我并不十分关心IE10,但IE11和Edge的最新版本似乎都有这个问题,所以我认为这值得讨论,因为无论最新的蹩脚浏览器微软发布,哈哈。 – ndimatteo

+0

请注意,如果它是一个bug,MS自从鲍尔默被踢出后已经发生了相当大的变化,所以你可以在https://developer.microsoft.com/en-us上的* public issue tracker */microsoft-edge/platform/issues /这几天(这太棒了) –

回答

5

我明年方面的变化有reach good result与IE11。

加入SVG标记的版本和风格

<svg 
    viewBox="0 0 800 400" 
    version="1.1" 
    style=" 
     shape-rendering:auto; 
     text-rendering:auto; 
     image-rendering:auto; 
     fill-rule:evenodd; 
     clip-rule:evenodd" 
> 

而且在IE中启用仿真边缘模式

<head> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 

但是加入这个领域(codepen.io)/Settings/HTML/Stuff to <head>/头部没有变化,所以我通过F12/Document mode/Edge启用此模式(不要忘记按F5)。

我希望这会帮助你。

+0

谢谢!这实际上似乎工作得很好,除了最新版本的Edge(Edge 13)之外。似乎很奇怪,因为老版本的Edge和IE10-11都在工作。任何想法为什么它仍然在边缘13裁剪? – ndimatteo

+0

我没有任何想法...关于微软的浏览器和svg渲染存在一些[错误报告](https://connect.microsoft.com/IE/Feedback/Details/1571965),但有了新的更新,我们得到了新的错误这个愤怒的公司。把我从CorelDraw创建的svg中获得的版本和风格窍门(作为一种很好的方法)。在全球范围内,所有试图解决这一过程的尝试都是魔术和不良之处。 – imbearr

+0

实际上,刚刚在项目中测试过它,现在它在Edge 13中工作,必须与您提到的元标记有关!这很完美,非常感谢! – ndimatteo