简体   繁体   English

如何使用jQuery动画滚动

[英]How to scroll with jquery animate

I'm trying to make some buttons work here. 我正在尝试使某些按钮在这里工作。 http://www.sepulturaimpex.ro/portofoliu is the website. http://www.sepulturaimpex.ro/portofoliu是网站。

When i click left/right buttons i'd like to move from project to project exactly 当我单击向左/向右按钮时,我想从一个项目移到另一个项目

The images are random width. 图像是随机宽度。

How can i achieve that? 我该如何实现?

Here is the script i'm using. 这是我正在使用的脚本。

$(document).ready(function () {
    $(".prev").click(function () {
        $(".p_horizontal_wrap").animate({
            scrollLeft: "-=700"
        })
    }), $(".next").click(function () {
        $(".p_horizontal_wrap").animate({
            scrollLeft: "+=700"
        })
    })
}),

The answer is in your question; 答案就在您的问题中; if the images are random width, then you cannot scroll w/ a fixed width 如果图像是随机宽度,则无法以固定宽度滚动

I think your best bet is to look ahead and find the x position of the next object, then scroll to that. 我认为最好的选择是向前看并找到下一个对象的x位置,然后滚动到该位置。 Depending on your markup, you may need to keep track of the object index you're scrolling into view. 根据您的标记,您可能需要跟踪滚动到视图中的对象索引。

Your next button (and your next/prev could be the same) would look like this: 您的下一个按钮(下一个/上一个可以相同)如下所示:

$(".next").click(function() {
    var targ = /** find out the next item to be shown **/
    var left = $(targ).position().left;
    $(".p_horizontal_wrap").animate({
        scrollLeft: left
    });
});

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

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