简体   繁体   English

jQuery幻灯片代码无法正常工作

[英]jQuery slideshow code won't work

I'm building a simple jQuery slideshow that I'm using for my website. 我正在构建一个用于我的网站的简单jQuery幻灯片。

I found the code here (and did some adjustments): http://www.barrelny.com/blog/building-a-jquery-slideshow-plugin-from-scratch/ (my only requirements were that the code had to be built from scratch, be lightweight, use CSS3 Transitions and slide automatically every fifth second.) 我在这里找到了代码(并做了一些调整): http : //www.barrelny.com/blog/building-a-jquery-slideshow-plugin-from-scratch/ (我唯一的要求是必须构建代码从头开始,要轻巧,使用CSS3 Transitions并每五秒钟自动滑动一次。)

The problem is, I can't even get anything to happen with my code. 问题是,我的代码什至无法发生任何事情。 Anyways, here it is. 无论如何,这是。

(function($){
function prefix(el){
    var prefixes = ["webkit", "moz", "o", "ms"];
    for (var i = 0; i < prefixes.length; i++){
        if (prefixes[i] + "transition" in el.style){
            return '-'+prefixes[i].toLowerCase()+'-'; 
        };
    }; 
    return "transition" in el.style ? "" : false;
};
s    var methods = {
    init: function(settings){
        return this.each(function(){
            var config = {
                slideDur: 7000,
                fadeDur: 800
            };
            if(settings){
                $.extend(config, settings);
            };
            this.config = config;
            var $container = $(this),
                slideSelector = 'article',
                fading = false,
                slideTimer,
                activeSlide,
                newSlide,
                $slides = $container.find(slideSelector),
                totalSlides = $slides.length,
                $pagerList = $container.find('#control');
                prefix = prefix($container[0]);
            function waitForNext(){
                slideTimer = setTimeout(function(){
                    changeSlides('next');
                },config.slideDur);
            };
            function animateSlides(activeNdx, newNdx){
                function cleanUp(){
                    $slides.eq(activeNdx).removeAttr('style');
                    activeSlide = newNdx;
                    fading = false;
                    waitForNext();
                };
                if(fading || activeNdx == newNdx){
                    return false;
                };
                fading = true;
                $pagers.removeClass('active').eq(newSlide).addClass('active');
                $slides.eq(activeNdx).css('z-index', 3);
                $slides.eq(newNdx).css({
                    'z-index': 2,
                    'opacity': 1
                });
                var styles = {};
                styles[prefix+'transition'] = 'opacity '+config.fadeDur+'ms';
                styles['opacity'] = 0;
                $slides.eq(activeNdx).css(styles);
                var fadeTimer = setTimeout(function(){
                    cleanUp();
                },config.fadeDur);
            };
            function changeSlides(target){
                if(target == 'next'){
                    newSlide = activeSlide + 1;
                    if(newSlide > totalSlides - 1){
                        newSlide = 0;
                    }
                } else if(target == 'prev'){
                    newSlide = activeSlide - 1;
                    if(newSlide < 0){
                        newSlide = totalSlides - 1;
                    };
                } else {
                    newSlide = target;
                };
                animateSlides(activeSlide, newSlide);
            };
            for(var i = 0; i < totalSlides; i++){
                $pagerList
                    .append('<nav id="control" data-target="'+i+'">'+i+'</nav>');
            };
            $container.find('#control').bind('click',function(){
                var target = $(this).attr('data-target');
                clearTimeout(slideTimer);
                changeSlides(target);
            });
            var $pagers = $pagerList.find('#control');
            $slides.eq(0).css('opacity', 1);
            activeSlide = 0;
            slideTimer = setTimeout(function(){
                changeSlides('next');
            },config.slideDur);
        });
    }
};
$.fn.easyFader = function(settings){
    return methods.init.apply(this, arguments);
};
})(jQuery);

And here's the fiddle: http://jsfiddle.net/b7PLz/ 这是小提琴: http : //jsfiddle.net/b7PLz/

I really hope somebody could help me - any help will be appreciated. 我真的希望有人能帮助我-任何帮助将不胜感激。 Thanks 谢谢

I've updated your jsfiddle and now it works perfectly. 我已经更新了您的jsfiddle ,现在可以正常使用了。 Your problem, apart from a typo in the code (there was an 's' before 'var methods') and that you were executing the plugin function in the DOMready event handler and invoking it at the end of the body tag, was that your slides were arranged vertically inside the slideshow container. 除了代码中的错字(在“ var方法”之前有一个“ s”),而且您正在DOMready事件处理程序中执行插件功能并在body标记的末尾调用它之外,您的问题是幻灯片垂直放置在幻灯片容器中。 As the slideshow container has a specific height and overflow hidden, they were invisible. 由于幻灯片放映容器具有特定的高度并隐藏了溢出,因此它们是不可见的。 I changed that, setting the articles to position absolute so the articles ar now one upon the other. 我改变了这一点,将文章设置为绝对位置,这样文章现在就可以彼此相对。 Besides, without position absolute or relative z-index doesn't work. 此外,没有位置,绝对或相对z-index无效。

#slideshow article { 
  height: 345px; 
  width: 700px; 
  position: absolute; 
  top: 0px; 
  left: 0px; 
}

I open a second answer to post the new code. 我打开第二个答案以发布新代码。

I've updated the jsfiddle , now the transition is a slide, and the code is simpler: 我已经更新了jsfiddle ,现在过渡是一张幻灯片,并且代码更简单:

(function($){
function prefix(el){
    var prefixes = ["webkit", "moz", "o", "ms"];
    for (var i = 0; i < prefixes.length; i++){
        if (prefixes[i] + "transition" in el.style){
            return '-'+prefixes[i].toLowerCase()+'-'; 
        };
    }; 
    return "transition" in el.style ? "" : false;
};
var methods = {
    init: function(settings){
        return this.each(function(){
            var config = {
                slideDur: 7000,
                fadeDur: 800
            };
            if(settings){
                $.extend(config, settings);
            };
            this.config = config;
            var $container = $(this),
                slideSelector = 'article',
                slideTimer,
                activeSlide,
                $slides = $container.find(slideSelector),
                $carousel = $slides.parent();
                totalSlides = $slides.length;
                $carousel.css('width', (totalSlides * 100) + '%');
                $pagerList = $container.find('#control');
                prefix = prefix($container[0]);
            function waitForNext(){
                slideTimer = setTimeout(function(){
                    changeSlides('next');
                },config.slideDur);
            };
            function animateSlides(){
                $pagers.removeClass('active').eq(newSlide).addClass('active');
                var $slide = $slides.eq(newSlide);
                var offset = $slide.position().left;
                console.log(offset);
                var styles = {};
                styles[prefix+'transition-duration'] = config.fadeDur+'ms';
                styles[prefix+'transform'] = 'translate3d(-'+offset+'px, 0px, 0px)';
                console.log(styles);
                $carousel.css(styles);
                waitForNext();
            };
            function changeSlides(target){
                if(target == 'next'){
                    newSlide = activeSlide + 1;
                    if(newSlide >= totalSlides){
                        newSlide = 0;
                    }
                } else if(target == 'prev'){
                    newSlide = activeSlide - 1;
                    if(newSlide < 0){
                        newSlide = totalSlides - 1;
                    };
                } else {
                    newSlide = Math.max(0, Math.min(target, totalSlides - 1));
                };
                activeSlide = newSlide;
                animateSlides();
            };
            for(var i = 0; i < totalSlides; i++){
                $pagerList
                    .append('<nav id="control" data-target="'+i+'">'+i+'</nav>');
            };
            $container.find('#control').bind('click',function(){
                var target = $(this).attr('data-target');
                clearTimeout(slideTimer);
                changeSlides(target);
            });
            var $pagers = $pagerList.find('#control');
            activeSlide = 0;
            slideTimer = setTimeout(function(){
                changeSlides('next');
            },config.slideDur);
        });
    }
};
$.fn.easyFader = function(settings){
    return methods.init.apply(this, arguments);
};
console.log('here');

})(jQuery); })(jQuery的);

You have to wrap the articles in another div with id 'carousel'. 您必须将文章包装在另一个ID为“ carousel”的div中。

Right now, when the slideshow reaches the last slide, it goes back to the beginning. 现在,当幻灯片放映到最后一张幻灯片时,它会回到开头。 If you want to always advance from right to left, and go back from left to right, it becomes a bit more complex. 如果您想始终从右向左前进,然后从左向右前进,则变得有些复杂。 You could try bxSlider , that already allows you to do this, with many more options. 您可以尝试bxSlider ,它已经允许您执行此操作,还有更多选择。

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

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