简体   繁体   English

问题设置与jQuery的CSS

[英]problems setting css with jquery

I have created a slideshow with jquery. 我用jquery创建了一个幻灯片。 It clones an image in a container, moves it to the right, then slides it to the left and starts over. 它将图像克隆到容器中,将其向右移动,然后向左滑动,然后重新开始。 Here is the code: 这是代码:

$(document).ready(function() {
    var slideshow = new main.slideshow();
    slideshow.start({
        path: 'images/slideshow/',
        images: ['1', '2']
    });
});

var main = new (function() {

    this.slideshow = (function() {
        var self = this;
        var nextSlide, path, images, startLeft;
        var fileExtension = 'jpg';
        var container = $('#slideshow');
        var currentSlide = container.children('img');
        var timerlength = 4000;
        var currentSlideIndex = 0;

        this.start = function(args) {
            path = args['path'];
            images = args['images'];
            if (typeof args['fileExtension'] !== 'undefined') fileExtension = args['fileExtension'];
            container.css('overflow', 'hidden');
            currentSlide.css('position', 'absolute');
            startLeft = currentSlide.position();
            startLeft = startLeft.left;
            self.nextSlide();
        };

        this.nextSlide = function() {
            nextSlide = currentSlide.clone();
            nextSlide.css('left', (startLeft + currentSlide.width()) + 'px');
            currentSlideIndex++;
            if (currentSlideIndex >= images.length) currentSlideIndex = 0;
            nextSlide.attr('src', path + images[currentSlideIndex] + '.' + fileExtension);
            container.append(nextSlide);
            setTimeout(function() {
                self.slideToNext();
            }, timerlength);
        };

        this.slideToNext = function() {
            currentSlide.animate({
                left: '-' + (currentSlide.width() - startLeft) + 'px'
            }, 2000);
            nextSlide.animate({
                left: startLeft + 'px'
            }, 2000, function() {
                currentSlide.remove();
                currentSlide = nextSlide;
                self.nextSlide();
            });
        };
    });
});

A link to see this in action can be found here: https://dustinhendricks.com/breastfest/public_html/ 可以在此处找到查看此操作的链接: https : //dustinhendricks.com/breastfest/public_html/

The problem I'm having as you can see is that the second slide when first added to the dom, does not seem to be moved to the right when I call css('left', x); 如您所见,我遇到的问题是,当第二张幻灯片首次添加到dom时,当我调用css('left', x);时似乎没有移到右边css('left', x); . After the first jQuery animation however, each cloned slide then seems to be able to be moved to the right with that call no problem. 但是,在第一个jQuery动画之后,每个克隆的幻灯片似乎都可以向右移动,而调用没有问题。 This leads me to believe that jquery's animate is setting something that allows for the object to be moved via css('left', x); 这使我相信jquery的动画设置了一些允许通过css('left', x);移动对象的东西css('left', x); , but what could it be changing? ,但可能会发生什么变化? position is already being set to absolute. 位置已经设​​置为绝对值。

This is why my example pages seems to take a while before the slides start sliding. 这就是为什么我的示例页面在幻灯片开始滑动之前需要花费一些时间的原因。 Any idea how I can fix? 知道我该如何解决吗?

If your first image is not loaded yet when you call .start() such that currentslide.width() isn't correct, then it won't set the proper initial value for left upon initialization. 如果你的第一个图像尚未加载,当你调用.start()使得currentslide.width()是不正确的,那么它不会设置适当的初始值left在初始化。 You may need to set a .load() event handler so you know when that first slide is loaded and you can wait for it to be loaded before starting the slideshow. 您可能需要设置.load()事件处理程序,以便知道何时加载第一张幻灯片,并且可以在开始幻灯片放映之前等待其加载。

When testing this, you must set the .load() handler before the .src value is set on the image object (or you may miss the load event in IE) and you should make sure you test both the cases where no images are cached and where all images are cached (as the timing of the load event can be different in both cases). 测试时,必须在图像对象上设置.src值之前设置.src .load()处理程序(否则您可能会错过IE中的load事件),并且应确保同时测试了没有缓存图像的两种情况以及缓存所有图片的位置(因为两种情况下加载事件的时间可能会有所不同)。

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

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