使用 .animate() 单击滑动 div:如何限制位移?

发布于 2024-09-15 21:01:55 字数 985 浏览 2 评论 0原文

我用 jQuery 创建了一个非常基本的滑块。有两个箭头称为 .theleft 和 .theright,单击时会移动一些 div 内容,因此充满图像的水平图库会在 950 像素范围内从一侧移动到另一侧。这是代码:

$(".theright").click(function() {               
$(".mymove").animate({ 
        left: "-=950px",
      }, 1500 );
});

$(".theleft").click(function() {                
$(".mymove").animate({ 
        left: "+=950px",
      }, 1500 );
});

HTML 非常简单:

<div class="mymove" style="position:relative">
    <ul>
        <li>
            <img src="" title="" alt="">
        </li>
        <li>
            <img src="" title="" alt="">
        </li>
        <li>
            <img src="" title="" alt="">
        </li>
        <li>
            <img src="" title="" alt="">
        </li>
    </ul>
</div>

我的问题是,当页面加载时,如果我做的第一件事是单击向左箭头,图库就会离开视口,在右侧迷失方向,也就是说,我还没有没有找到“限制”滑块动作的方法...

您知道有什么方法可以改进吗?

非常感谢。

I've created a very basic slider with jQuery. There are two arrows called .theleft and .theright, which are moving some div contents when clicked, so an horizontal gallery full of images moves from side to side at a 950px range. This is the code:

$(".theright").click(function() {               
$(".mymove").animate({ 
        left: "-=950px",
      }, 1500 );
});

$(".theleft").click(function() {                
$(".mymove").animate({ 
        left: "+=950px",
      }, 1500 );
});

The HTML is very simple:

<div class="mymove" style="position:relative">
    <ul>
        <li>
            <img src="" title="" alt="">
        </li>
        <li>
            <img src="" title="" alt="">
        </li>
        <li>
            <img src="" title="" alt="">
        </li>
        <li>
            <img src="" title="" alt="">
        </li>
    </ul>
</div>

My question is, when the page loads, if the first thing I do is clicking the left arrow, the gallery gets off the viewport getting lost at the right side, that is, I haven't found a way to 'limit' the slider action...

Do you know of some way to improve that?

Many thanks.

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

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

发布评论

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

评论(1

诗化ㄋ丶相逢 2024-09-22 21:01:55

您将需要某种计数器来跟踪滑块的位置。

因此,如果它最初从 0 开始,则如下所示:

$(".theleft").click(function() {                
    if (slider_pos > 0) {
        $(".mymove").animate({ 
            left: "+=950px",
        }, 1500 );
        // Then update slider_pos here i.e. view_pos += 950;
    }
});

You will need some sort of counter that will keep track of where your slider is up to.

So if it initially starts at 0 have something like this:

$(".theleft").click(function() {                
    if (slider_pos > 0) {
        $(".mymove").animate({ 
            left: "+=950px",
        }, 1500 );
        // Then update slider_pos here i.e. view_pos += 950;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文