简体   繁体   English

上一张幻灯片后如何停止动画?

[英]How to stop animation after last slide?

I have created custom plugin for image slider. 我已经为图像滑块创建了自定义插件。 It is like pass parameter of no of images to display. 就像显示没有图像的传递参数。 How to stop animation once the last image is visible? 一旦看到最后一张图像,如何停止动画播放? Sometimes issue with previous click also. 有时也与先前的点击有关。 Help me to optimize code. 帮我优化代码。

CSS CSS

.imgpresentation {
width: 520px;
height: 110px;
overflow: hidden;
border: 1px solid #ddd;
position: relative;
}
.imgpresentation img {
 float: left;
 padding: 5px;
}
.imgpresentation .prev {
left: 0;
position: absolute;
top: 40%;
}
.imgpresentation .next {
right: 0;
position: absolute;
top: 40%;
 }
.slidethemimgpresentation {
position: absolute;
}

JS JS

<script>
$.fn.imgSlider = function(nImages) {
var imgW = $(this).find('img').outerWidth(),
    imgH = $(this).find('img').outerHeight(),
    imgL = $(this).find('img').length,
    that = $(this),
    container = that.find('.slidethemimgpresentation');

that.append('<a href="#" class="prev">Previous</a>'+
' <a href="#" class="next">Next</a>').css({
    'width': nImages * imgW,
    'height': imgH
});
container.css({
    'width': imgW * imgL
});

imgW = nImages * imgW;

that.find('.next').on('click', function() {
    var currLeft = parseInt(container.css('left'));
    currLeft += -imgW;
    container.animate({
        'left': currLeft
    });
    return false;
});
that.find('.prev').on('click', function() {
    var currLeft = parseInt(container.css('left'));
    if (currLeft == 0) {
        return false;
    }
    currLeft += imgW;

    container.animate({
        'left': currLeft
    });
    return false;
});
};
$(document).ready(function() {
  $('#imgpresentation').imgSlider(2);
  $('#imgpresentation2').imgSlider(3);
  $('#imgpresentation3').imgSlider(4);
  $('#imgpresentation4').imgSlider(1);
});
</script>

html ` html`

<div id="imgpresentation4" class="imgpresentation">
    <div class="slidethemimgpresentation">
        <img class="presentatinthis" src="http://placehold.it/150x150&text=Slide301" />
        <img class="presentatinthis" src="http://placehold.it/150x150&text=Slide302" />
        <img class="presentatinthis" src="http://placehold.it/150x150&text=Slide303" />
        <img class="presentatinthis" src="http://placehold.it/150x150&text=Slide304" />
        <img class="presentatinthis" src="http://placehold.it/150x150&text=Slide305" />
    </div>
</div>
<div id="imgpresentation" class="imgpresentation">
    <div id="slidethemimgpresentation" class="slidethemimgpresentation">
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide101" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide102" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide103" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide104" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide105" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide106" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide107" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide108" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide109" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide110" />
    </div>
</div>
<div id="imgpresentation2" class="imgpresentation">
    <div class="slidethemimgpresentation">
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide201" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide202" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide203" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide204" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide205" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide206" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide207" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide208" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide209" />
        <img class="presentatinthis" src="http://placehold.it/250x100&text=Slide210" />
    </div>
</div>
<div id="imgpresentation3" class="imgpresentation">
    <div class="slidethemimgpresentation">
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide301" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide302" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide303" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide304" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide305" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide306" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide307" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide308" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide309" />
        <img class="presentatinthis" src="http://placehold.it/350x150&text=Slide310" />
    </div>
</div>

Thats a slightly tricky way of doing it. 那是一个有点棘手的方法。 Because it's not being driven by an array, rather moving container += X each time. 因为它不是由数组驱动的,所以每次都移动容器+ =X。

You'll need to check for if( container.style.left + imgW > totalWidth ) To debug try using console.log( container.style.left ) 您需要检查是否if( container.style.left + imgW > totalWidth )要进行调试,请尝试使用console.log( container.style.left )

I'd reccomend using an existing slider, iDangerio.us Swiper is really good with a great API. 我建议使用现有的滑块,iDangerio.us Swiper的API很好。

Also on a side note, this is slightly inefficient, not that it matters too much because it's only being run on init. 另外,这有点低效,不是太重要,因为它仅在init上运行。

var imgW = $(this).find('img').outerWidth(),
imgH = $(this).find('img').outerHeight(),
imgL = $(this).find('img').length,
that = $(this),
container = that.find('.slidethemimgpresentation')

It would be better practice and more efficient to only parse the jquery object once. 只解析一次jquery对象将是一种更好的做法,并且效率更高。

var that = $(this),
img = that.find('img'),
imgW = img.outerWidth(),
imgH = img.outerHeight(),
imgL = img.length,
container = that.find('.slidethemimgpresentation'),
totalWidth = imgW * imgL;

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

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