简体   繁体   English

jQuery在位置.animation(..)之后通过.css(..)将div位置更改

[英]JQuery change div position by .css(..) after position .animation(..)

I have a js code for slideshow: 我有一个幻灯片的js代码:

var frameWidth = 0;
var slideCount = $this.children(".slide-frame").length;
var $currentFrame, $nextFrame;
var renderNext = function () {
    var moveCount = 0;
    // get next frame
    if ($currentFrame.next(".slide-frame").length === 0) {
        $nextFrame = $(".slide-frame").eq(0);
    } else {
        $nextFrame = $currentFrame.next(".slide-frame");
    }
    // move next frame to the right before current frame
    $nextFrame.css({"display": "block"}, {"left": frameWidth}); // why this string works once on first timer tick???
    // all frames move animation
    $(".slide").children(".slide-frame").animate({"left": "-=" + frameWidth}, 700, function () {
        moveCount++;
        // wait for all frames animation complete
        if (moveCount === slideCount) {
            // make new current frame from next frame
            $currentFrame = $nextFrame;
        }
    });
};

frameWidth = $(".slide").width(); // get frame width
$(".slide").children(".slide-frame").width(frameWidth); // set fixed width for all frames
$currentFrame = $(".slide").children(".slide-frame").eq(0); // get first current frame
$currentFrame.css({"display": "block"});

if (slideCount > 0) { // run slide show
    setInterval(renderNext, 2000);
}

and CSS/LESS: 和CSS / LESS:

.slide{
    overflow: hidden;
    position: relative;
    .slide-frame{
        position: absolute;
        top: 0px;
        left: 0px;
        display: none;
    }
} 

HTML: HTML:

<div class="slide">
    <a class="slide-frame" id="frame-00" href="#">
        <img class="frame-image" alt="" src="/img/slide-index/slide-frame-0.jpg">
    </a>
    <a class="slide-frame" id="frame-01" href="#">
        <img class="frame-image" alt="" src="/img/slide-index/slide-frame-1.jpg">
    </a>
</div>

String $nextFrame.css({"display": "block"}, {"left": frameWidth}); 字符串$nextFrame.css({"display": "block"}, {"left": frameWidth}); works only on first timer "tick" and all frames move left without correct $nextFrame position changing (it mast move to the right before $currentFrame). 仅在第一次计时器“ tick”上起作用,并且所有帧向左移动而没有正确的$ nextFrame位置更改(它的桅杆在$ currentFrame之前向右移动)。

Why object css parameter "left" is not change? 为什么对象css参数“ left”不变?

Problem solved! 问题解决了!

Element ".slide" and its cildren inherited CSS transition properties. 元素“ .slide”及其cildren继承了CSS过渡属性。 JQuery animate() does not work correctly simultaneously with CSS transitions jQuery animate()与CSS转换无法同时正常工作

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

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