Jquery:让它继续选择下一张图像,而不必输入“.next().next().next() 等

发布于 2024-09-09 08:40:44 字数 812 浏览 5 评论 0原文

使用我的代码,我可以让一个 div 滚动浏览 4 个图像,但如果你看一下代码,我必须继续添加“.next”才能让它滚动到另一个图像。我不想确定在 jquery 中滚动的图像数量,我希望 jquery 继续在 div 中查找尽可能多的图像,滚动浏览所有图像,然后滚动回顶部。看看我的代码。我不想使用其他插件,因为与多 KB 插件相比,我几乎只用几行代码就完成了我想要做的事情。

ps:我不知道为什么“-165px”是必要的,但它使得在从图片滚动到图片时一切都完美对齐。

Jquery:

Jquery:

$(document).ready(function(){
    $('.rotate_container').everyTime(10, function (){
        $('.rotate_container').animate({scrollTop: $('img').offset().top - 165}, 2000).delay(1000)
        .animate({scrollTop: $("img").next().offset().top - 165}, 2000).delay(1000)                   
        .animate({scrollTop: $("img").next().next().offset().top - 165}, 2000).delay(1000)
        .animate({scrollTop: $("img").next().next().next().offset().top - 165}, 2000).delay(1000)
    });
});

有什么想法吗?

With my code I can get a div to scroll through 4 images, but if you look at the code, i have to keep adding '.next' to get it to scroll to another image. I don't want to determine the amount of images to scroll through in jquery, i want jquery to keep looking through the div for as many images as i have, scroll through all of them, then scroll back to the top. just take a look at my code. I don't want to use another plugin because i've almost accomplished what I want to do with just a few lines of code compared to multi KB plugins.

ps: I don't know why the '-165px' is necessary, but it makes it all align perfectly when scrolling from picture to picture.

Jquery:

Jquery:

$(document).ready(function(){
    $('.rotate_container').everyTime(10, function (){
        $('.rotate_container').animate({scrollTop: $('img').offset().top - 165}, 2000).delay(1000)
        .animate({scrollTop: $("img").next().offset().top - 165}, 2000).delay(1000)                   
        .animate({scrollTop: $("img").next().next().offset().top - 165}, 2000).delay(1000)
        .animate({scrollTop: $("img").next().next().next().offset().top - 165}, 2000).delay(1000)
    });
});

Any ideas?

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

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

发布评论

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

评论(5

2024-09-16 08:40:44

我没有测试过,但这似乎会起作用

$(document).ready(function(){
    var container = $('.rotate_container');
    var images = container.find('img');
    container.everyTime(10, function (){
        images.each(function() {
            container.animate({scrollTop: $(this).offset().top - 165}, 2000).delay(1000);
        });
    });
});

I didn't test it, but this seems like it will work

$(document).ready(function(){
    var container = $('.rotate_container');
    var images = container.find('img');
    container.everyTime(10, function (){
        images.each(function() {
            container.animate({scrollTop: $(this).offset().top - 165}, 2000).delay(1000);
        });
    });
});
寻找一个思念的角度 2024-09-16 08:40:44

可能会看看 jCarousel 或其弟弟 jCarouselLite,它可以轻松实现图像滚动效果。

对于上述问题的解决方案,您可以将第一个 img 分配给变量,然后在回调中递增它。在下面的代码中,警报输出是“第二”:

<div>First</div>
<div>Second</div>
<script type="text/javascript">
$(document).ready(function(){
    var test = $('div');
        test = test.next();
        alert(test.html());
});
</script>

因此,要修改您的代码:

$(document).ready(function(){
    var img = $('.rotate_container img');
    $('.rotate_container').everyTime(10, function (){
        $('.rotate_container').animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
        .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
        .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
          .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
    });
});

等等。

可以使用函数对其进行修改,以使其更加清晰。

Might check out jCarousel or it's little brother, jCarouselLite, which do image scrolling with ease effects.

For a solution to the above, you might assign the first img to variable and then increment it on the callback. In the following code the alert output is 'Second':

<div>First</div>
<div>Second</div>
<script type="text/javascript">
$(document).ready(function(){
    var test = $('div');
        test = test.next();
        alert(test.html());
});
</script>

So, to modify your code:

$(document).ready(function(){
    var img = $('.rotate_container img');
    $('.rotate_container').everyTime(10, function (){
        $('.rotate_container').animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
        .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
        .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
          .animate({scrollTop: img.offset().top - 165}, 2000,null,function() { img = img.next(); }).delay(1000)
    });
});

etc.

Can probably modify that using a function to make it even cleaner as well.

梦忆晨望 2024-09-16 08:40:44

递归。编写一个函数来执行您想要的操作(我会帮助编写特定代码,但我不知道您要做什么)。让函数获取第一张图像。在函数中执行您想要的操作,在关闭函数之前调用您所在的函数并向其发送 .next() 变量。

Recursion. Write a function to do what you want (I would help with specific code but I don't know what you are going for). Have the function take in the first image. Do what you want in the function, and before you close the function call the function you are in and send it the .next() variable.

渡你暖光 2024-09-16 08:40:44

我的解决方案要求您在容器中添加一个额外的 div 和一个 css 规则。它是这样的:

<script type="text/javascript">
$(document).ready(function(){
    var top = 0;
    var up = true;
    var wrap = $(".rotate_container > div ") // shortcut :)

    function slide() {
        wrap.animate( { "top" : top+"px" }, 400 );

        if( Math.abs( top ) > wrap.height() ){ // if all inner_wrap is outside container
            up = false; 
        }
        else if( top >= 0 ) {
            up = true
        }
        if( up ) top -= 165;
        else top += 165;

        setTimeout( slide, 500 ) // call function slide after 500 ms
    }
    setTimeout( slide, 500 ) // call function slide after 500 ms
});
</script>

<style type="text/css">
.rotate_container {
    max-height:292px;
    max-width:760px;
    height:292px;
    width:760px;
    overflow:hidden;
    padding:0;
    margin:0;}

.rotate_container > div {
    position: relative;
    top: 0; left: 0;
}

.rotate_container img {
    height:292px;
    width:760px;}
</style>


<div class="rotate_container grid_12">
    <div id="inner_wrap">
        <img src="{{ MEDIA_URL }}/employlogo.png" class="top" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="middle" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="midbottom" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="bottom" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="verybottom" />
    </div>
</div>

嗯,它不漂亮,但它有效。我一开始误解了你的片段,抱歉。

My solution requires you to put one additional div inside your container and one css rule. It goes like this:

<script type="text/javascript">
$(document).ready(function(){
    var top = 0;
    var up = true;
    var wrap = $(".rotate_container > div ") // shortcut :)

    function slide() {
        wrap.animate( { "top" : top+"px" }, 400 );

        if( Math.abs( top ) > wrap.height() ){ // if all inner_wrap is outside container
            up = false; 
        }
        else if( top >= 0 ) {
            up = true
        }
        if( up ) top -= 165;
        else top += 165;

        setTimeout( slide, 500 ) // call function slide after 500 ms
    }
    setTimeout( slide, 500 ) // call function slide after 500 ms
});
</script>

<style type="text/css">
.rotate_container {
    max-height:292px;
    max-width:760px;
    height:292px;
    width:760px;
    overflow:hidden;
    padding:0;
    margin:0;}

.rotate_container > div {
    position: relative;
    top: 0; left: 0;
}

.rotate_container img {
    height:292px;
    width:760px;}
</style>


<div class="rotate_container grid_12">
    <div id="inner_wrap">
        <img src="{{ MEDIA_URL }}/employlogo.png" class="top" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="middle" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="midbottom" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="bottom" />
        <img src="{{ MEDIA_URL }}/employlogo.png" class="verybottom" />
    </div>
</div>

Well, it's not pretty, but it works. I misunderstood your snippet at first, sorry.

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