简体   繁体   English

jQuery - 如何在单击div时让浏览器窗口滚动水平?

[英]jQuery - How to get browser window to scroll horizontal when div is clicked?

I have picture of an arrow in a div. 我有div中的箭头图片。 This div is fixed in the bottom right corner of very wide page. 这个div固定在非常宽的页面的右下角。

How can I use jQuery to scroll the window right 600px each time the div is clicked? 如何在每次单击div时使用jQuery将窗口向右滚动600px? (And is it possible to detect when the page can no longer scroll right, and hide the arrow?) (并且可以检测页面何时无法向右滚动,并隐藏箭头?)

Cheers. 干杯。

Try something like this: 尝试这样的事情:

var distance = 600;
$("div").click(function() {
    $("html:not(:animated), body:not(:animated)").animate(
        {scrollLeft: "+="+distance}, 400
    );
});

jsfiddle here: http://jsfiddle.net/juXLu/2/ jsfiddle: http//jsfiddle.net/juXLu/2/

[edit] And here's detecting if you're at the end of the document http://jsfiddle.net/lukemartin/juXLu/5/ [编辑]这里检测你是否在文件的最后http://jsfiddle.net/lukemartin/juXLu/5/

var distance = 600,
    docWidth = $(document).width(),
    scrollPos;

// click handler
$("div").click(function() {

    // animate 
    $("html:not(:animated), body:not(:animated)").animate(
        // amount to scroll
        {scrollLeft: "+=" + distance},
        // scroll speed (ms)
        400,
        // callback function
        function(){
            // check our scroll position
            scrollPos = $(window).width() + $(window).scrollLeft(); 
            // if it equals the doc width, we're at the end
            if(docWidth === scrollPos) {
                $("div").text("End of the line");
            }
        }
    );    

});

Use the jquery method scrollLeft 使用jquery方法scrollLeft

$(document).ready(function(){
    $(window).scrollLeft((Number($(window).scrollLeft())+600)+'px');
});

Something like that :) 这样的东西:)

You could user the Scrollto plugin, 您可以使用Scrollto插件,

http://plugins.jquery.com/project/ScrollTo http://plugins.jquery.com/project/ScrollTo

It is really easy to use, just use the documentation. 它非常易于使用,只需使用文档即可。 Then you could create a placeholder to determine whether or not its to the end of the page. 然后,您可以创建一个占位符,以确定它是否到页面的末尾。 Just stick the placeholder at the very end, and calculate the distance. 只需将占位符粘贴在最后,然后计算距离。

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

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