简体   繁体   English

css动画和jquery .load()在移动设备上口吃

[英]css animation and jquery .load() stuttering on mobile devices

First, sorry for my bad English. 首先,抱歉我的英语不好。 I have some CSS stroke-dasharray animations which will trigger if a button is clicked by adding a new css-class to a div . 我有一些CSS stroke-dasharray动画,如果通过向div添加新的css类来点击按钮,将会触发。 On top of that, a text-file is loaded using jquery .load() . 最重要的是,使用jquery .load()加载文本文件。 Sadly, on my mobile devices, the animation will stutter. 可悲的是,在我的移动设备上,动画会口吃。 I tried the 我试过了

transform: translateZ(0);
-webkit-transform: translateZ(0);

hack but it is still stuttering. 黑客,但它仍然是口吃。 I also put the .attr() stuff at the callback position of the .load() so its triggered after loading finished. 我也把.attr()的东西在的回调位置.load()所以它的触发后加载完毕。 But its still stuttering. 但它仍然口吃。

My Question: is there an alternative for jquery .load() which will not stutter on mobile devices? 我的问题:有没有jquery .load()的替代品,它不会在移动设备上口吃? Or are there any other optimizations? 或者还有其他优化吗?

My code so far: 我的代码到目前为止:

<div>
    <svg>
        <path id="path1" class="path_before" d=... />
    </svg>
</div>
<div id="container>
</div>

CSS: CSS:

.path_before
{
    stroke: none;
    fill: none;
}

.path_after
{
    stroke: #6699ff;
    stroke-width: 4px;
    stroke-dasharray: 1000;
    stroke-dashoffset: 1000;
    animation: dash 4s linear forwards;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

jquery: jQuery的:

var values = "0;1;2;3;4;5;6".split(";"),
    currentIndex = 0;

function next() {
    $("#container").load("content/content.txt #schritt" + values[currentIndex], function(){    
        $("#path" + values[currentIndex]).attr( "class", "path_after");
        currentIndex = currentIndex + 1
    });
};

$(document).ready(function(){
    $("#next").click(next);
});

You want to avoid adding CSS that will causes a repaint of the page. 您希望避免添加会导致重新绘制页面的CSS。 When you add the stroke, that has some size. 添加笔划时,会有一些大小。 Even if you don't see anything on the page moves, it must be recalculated. 即使您在页面上看不到任何内容,也必须重新计算。

Instead of add/removing the stroke, try just making it visible and invisible. 而不是添加/删除笔划,尝试使其可见和不可见。 Making the stroke match the background color might help. 使笔划与背景颜色匹配可能会有所帮助。

.path 
{
        stroke: #6699ff;
        stroke-width: 4px;
        stroke-dasharray: 1000;
        stroke-dashoffset: 1000;
        animation: dash 4s linear forwards;
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
}
.path.path_before
{
        -webkit-text-stroke: 1px white;
}

.path.path_after
{
        -webkit-text-stroke: 1px black;
}

It's often possible to optimize jQuery code by writing vanilla JS, but the improvements are typically marginal, and the result is typically more difficult to maintain. 通常可以通过编写vanilla JS来优化jQuery代码,但这些改进通常是微不足道的,结果通常更难以维护。 Optimize the custom code first. 首先优化自定义代码。

In this case, fixing the janky stutter will probably have more to do with utilizing hardware-accelerated CSS than optimizing the jQuery code. 在这种情况下,修复janky口吃可能更多地与利用硬件加速的CSS相比,而不是优化jQuery代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM