JQuery 无限滑块 - 模拟 MacApp Store

发布于 2024-12-12 10:28:32 字数 1000 浏览 0 评论 0原文

如果有人知道 mac 应用商店上的滑块,那么这就是我重新创建的。但存在三个问题。

  • 右侧滑块和主推子上的滑块之间存在接缝动画延迟
  • insertAfter 函数什么也不做
  • 滑块不是无限循环

我已经设置了一个小提琴来测试是否有人可以解决它。 http://jsfiddle.net/Z5uER/2/

$(document).ready(function(){

    $('.sismain a').css('opacity', 0);

    var slideqnt = $('.sismain a').length;
    var slidecur = 0;
    var slidelay = 0;
    var slidemove = 0;
        $('.sismain a').each(function(){
            $(this).delay(slidelay).animate({opacity: 1,  leaveTransforms:true}, {duration:2000, queue:true});
            slidemove -=167;
            $('.siscolin').delay(slidelay - 2000).animate({top: slidemove,  leaveTransforms:true}, {duration:2000, queue:true});
            slidelay += 6000;
            $('.siscolin a:first').insertAfter('.siscolin a:last');
            $(this).delay(slidelay).animate({opacity: 0,  leaveTransforms:true}, {duration:2000, queue:true});
        });
});

If anyone knows the slider on the mac app store then this is what I have recreated. Three problems though.

  • There seams to be an animation delay between the slider on the right and that on the main fader
  • The insertAfter function is doing nothing
  • The slider is not on an infinite loop

I have set up a fiddle for testing if anyone can solve it. http://jsfiddle.net/Z5uER/2/

$(document).ready(function(){

    $('.sismain a').css('opacity', 0);

    var slideqnt = $('.sismain a').length;
    var slidecur = 0;
    var slidelay = 0;
    var slidemove = 0;
        $('.sismain a').each(function(){
            $(this).delay(slidelay).animate({opacity: 1,  leaveTransforms:true}, {duration:2000, queue:true});
            slidemove -=167;
            $('.siscolin').delay(slidelay - 2000).animate({top: slidemove,  leaveTransforms:true}, {duration:2000, queue:true});
            slidelay += 6000;
            $('.siscolin a:first').insertAfter('.siscolin a:last');
            $(this).delay(slidelay).animate({opacity: 0,  leaveTransforms:true}, {duration:2000, queue:true});
        });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2024-12-19 10:28:32

我创建了类似的功能。也许我会为此编写一个插件。

确保将 div 的所有尺寸设置为正确的值(以及脚本内的绝对定位):

HTML

<div class="slider">
    <div class="slides">
        <div><img src="http://dummyimage.com/180x90/000/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/f00/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/0f0/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/00f/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/f0f/fff" /></div>
    </div>
    <div class="thumbs">
        <div><img src="http://dummyimage.com/60x30/000/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/f00/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/0f0/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/00f/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/f0f/fff" /></div>
    </div>
</div>
<a id="pause" href="#">pause</a>

CSS

body {
    margin: 20px;
}
.slider {
    position: relative;
    width: 240px;
    height: 90px;
    overflow: hidden;
}
.slides div {
    position: absolute;
    top: 0;
    left: 0;
    width: 180px;
    height: 90px;
    z-index: 1;
    display: none;
}
.slides div.first-child {
    z-index: 2;
}
.thumbs {
    position: absolute;
    width: 60px;
    height: 90px;
    right: 0;
    top: 0;
}
.thumbs div {
    width: 60px;
    height: 30px;
    background-color: #f00;
    position: absolute;
    top: 0;
    right: 0;
}
#pause {
    background-color: #888;
    color: #fff;
    font-weight: bold;
    text-decoration: none;
    font-family: Arial;
    border: 1px solid #000;
    padding: 10px;
    margin-top: 20px;
    display: inline-block;
}

JavaScript

var intval = "";
var numberOfSlides = $('.slides > div').length;
var counter = 0;
var thumbs = $('.thumbs div').toArray();

$('.slides div:eq(0)').addClass('first-child');

$(thumbs).each(function(i,el){
    $(this).css({top: 90-(i*30+30) + 'px'});
});

var doShit = function() {
    if(counter == 0) {
        $('.slides div').eq((numberOfSlides-1)).fadeOut('fast');
    }
    else {
        $('.slides div').eq((counter-1)).fadeOut('fast');
    }

    $(thumbs).each(function(i,el){
        $(this).animate({top: '+=30'}, 1000, function(){
            if(i==0) {
                $(this).css({'top': '-60px'});
                            thumbs.push(thumbs.shift());
            }
        });
    });

    $('.slides div').eq(counter).fadeIn('fast');

    if(counter < (numberOfSlides-1)) {
        counter++;
    }
    else {
        counter=0;
    }
};

var start_Int = function() {
    if(intval==""){
        intval=window.setInterval(function() { doShit(); },2000);
    } else {
        stop_Int();
    }
};

var stop_Int = function() {
    if(intval!="") {
        window.clearInterval(intval);
        intval="";
    }
};

start_Int();

Fiddle : http://jsfiddle.net/dirkpennings/kf3v8/1/

我希望这会有所帮助。

I created a similar functionality. Maybe I'm going to write a plugin for this.

Be sure to set all the dimensions of your div's to the right values (and also the absolute positioning within the script):

HTML

<div class="slider">
    <div class="slides">
        <div><img src="http://dummyimage.com/180x90/000/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/f00/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/0f0/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/00f/fff" /></div>
        <div><img src="http://dummyimage.com/180x90/f0f/fff" /></div>
    </div>
    <div class="thumbs">
        <div><img src="http://dummyimage.com/60x30/000/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/f00/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/0f0/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/00f/fff" /></div>
        <div><img src="http://dummyimage.com/60x30/f0f/fff" /></div>
    </div>
</div>
<a id="pause" href="#">pause</a>

CSS

body {
    margin: 20px;
}
.slider {
    position: relative;
    width: 240px;
    height: 90px;
    overflow: hidden;
}
.slides div {
    position: absolute;
    top: 0;
    left: 0;
    width: 180px;
    height: 90px;
    z-index: 1;
    display: none;
}
.slides div.first-child {
    z-index: 2;
}
.thumbs {
    position: absolute;
    width: 60px;
    height: 90px;
    right: 0;
    top: 0;
}
.thumbs div {
    width: 60px;
    height: 30px;
    background-color: #f00;
    position: absolute;
    top: 0;
    right: 0;
}
#pause {
    background-color: #888;
    color: #fff;
    font-weight: bold;
    text-decoration: none;
    font-family: Arial;
    border: 1px solid #000;
    padding: 10px;
    margin-top: 20px;
    display: inline-block;
}

JavaScript

var intval = "";
var numberOfSlides = $('.slides > div').length;
var counter = 0;
var thumbs = $('.thumbs div').toArray();

$('.slides div:eq(0)').addClass('first-child');

$(thumbs).each(function(i,el){
    $(this).css({top: 90-(i*30+30) + 'px'});
});

var doShit = function() {
    if(counter == 0) {
        $('.slides div').eq((numberOfSlides-1)).fadeOut('fast');
    }
    else {
        $('.slides div').eq((counter-1)).fadeOut('fast');
    }

    $(thumbs).each(function(i,el){
        $(this).animate({top: '+=30'}, 1000, function(){
            if(i==0) {
                $(this).css({'top': '-60px'});
                            thumbs.push(thumbs.shift());
            }
        });
    });

    $('.slides div').eq(counter).fadeIn('fast');

    if(counter < (numberOfSlides-1)) {
        counter++;
    }
    else {
        counter=0;
    }
};

var start_Int = function() {
    if(intval==""){
        intval=window.setInterval(function() { doShit(); },2000);
    } else {
        stop_Int();
    }
};

var stop_Int = function() {
    if(intval!="") {
        window.clearInterval(intval);
        intval="";
    }
};

start_Int();

Fiddle: http://jsfiddle.net/dirkpennings/kf3v8/1/

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文