当光标位于屏幕的顶部或底部边缘时,如何使用 JQuery/Javascript 向下滚动页面?

发布于 2025-01-01 13:11:24 字数 1467 浏览 1 评论 0 原文

很简单,我只是想拥有它,以便当用户拖动项目并到达视口的最底部或顶部(10px左右)时,页面(大约 3000 像素长)轻轻地向下或向上滚动,直到将光标(以及被拖动的项目)移出该区域

项目是一个 li 标签,它使用 jquery 使列表项目可拖动。具体来说:

我目前使用 window.scrollBy(x=0,y=3) 滚动页面并具有以下变量:

  1. e .pageY ...提供绝对页面上光标的 Y 坐标(不相对于屏幕)
  2. $.scrollTop() ... 提供距页面顶部的偏移量(当滚动条一直向上时,为 0)
  3. $.height()... 提供用户浏览器/视口中可视区域的高度
  4. body.offsetHeight ...整个页面的高度

我怎样才能实现这个以及哪个事件最适合这个(当前它在鼠标悬停中)? 我的想法:

  1. 使用 if/else 来检查它是在顶部区域还是底部,如果 e.pageY 显示它在顶部,则向上滚动,如果 e.page& 则向下滚动。位于底部,然后调用 $('li').mouseover() 事件来迭代...
  2. 使用 do while 循环...实际上效果还不错,但很难阻止滚动到很远的地方。但我不知道如何控制迭代......

我的最新尝试:

          ('li').mouseover(function(e) {

            totalHeight = document.body.offsetHeight;
            cursor.y = e.pageY;
            var papaWindow = window;
            var $pxFromTop = $(papaWindow).scrollTop();
            var $userScreenHeight = $(papaWindow).height();
            var iterate = 0;

            do {
                papaWindow.scrollBy(0, 2);
                iterate++;
                console.log(cursor.y, $pxFromTop, $userScreenHeight);
            }

            while (iterate < 20);
      });

Simple, I just would like to have it so when a user is dragging an item and they reach the very bottom or top of the viewport (10px or so), the page (about 3000px long) gently scrolls down or up, until they move their cursor (and thus the item being dragged) out of the region.

An item is an li tag which uses jquery to make the list items draggable. To be specific:

I currently use window.scrollBy(x=0,y=3) to scroll the page and have the variables of:

  1. e.pageY ... provides absolute Y-coordinates of cursor on page (not relative to screen)
  2. $.scrollTop() ... provides offset from top of page (when scroll bar is all the way up, it is 0)
  3. $.height()... provides the height of viewable area in the user's browser/viewport
  4. body.offsetHeight ... height of the entire page

How can I achieve this and which event best accommodates this (currently its in mouseover)?
My ideas:

  1. use a an if/else to check if it is in top region or bottom, scroll up if e.pageY is showing it is in the top, down if e.page& is in bottom, and then calling the $('li').mouseover() event to iterate through...
    1. Use a do while loop... this has worked moderately well actually, but is hard to stop from scrolling to far. But I am not sure how to control the iterations....

My latest attempt:

          ('li').mouseover(function(e) {

            totalHeight = document.body.offsetHeight;
            cursor.y = e.pageY;
            var papaWindow = window;
            var $pxFromTop = $(papaWindow).scrollTop();
            var $userScreenHeight = $(papaWindow).height();
            var iterate = 0;

            do {
                papaWindow.scrollBy(0, 2);
                iterate++;
                console.log(cursor.y, $pxFromTop, $userScreenHeight);
            }

            while (iterate < 20);
      });

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

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

发布评论

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

评论(4

我是男神闪亮亮 2025-01-08 13:11:24

现在工作得很好,用户有时只需要在拖动项目时“摇动”鼠标即可保持滚动,但对于仅使用鼠标位置滚动来说,它非常稳定。这是我最终使用的:

 $("li").mouseover(function(e) {

  e = e || window.event; var cursor = { y: 0 }; cursor.y = e.pageY; //Cursor YPos
  var papaWindow = parent.window;
  var $pxFromTop = $(papaWindow).scrollTop();
  var $userScreenHeight = $(papaWindow).height();

  if (cursor.y > (($userScreenHeight + $pxFromTop) / 1.25)) {

         if ($pxFromTop < ($userScreenHeight * 3.2)) {

                   papaWindow.scrollBy(0, ($userScreenHeight / 30));
             }
        }
  else if (cursor.y < (($userScreenHeight + $pxFromTop) * .75)) {

        papaWindow.scrollBy(0, -($userScreenHeight / 30));

        }

   }); //End mouseover()

Works pretty well now, user just needs to "jiggle" the mouse when dragging items sometimes to keep scrolling, but for scrolling just with mouse position its pretty solid. Here is what I finally ended up using:

 $("li").mouseover(function(e) {

  e = e || window.event; var cursor = { y: 0 }; cursor.y = e.pageY; //Cursor YPos
  var papaWindow = parent.window;
  var $pxFromTop = $(papaWindow).scrollTop();
  var $userScreenHeight = $(papaWindow).height();

  if (cursor.y > (($userScreenHeight + $pxFromTop) / 1.25)) {

         if ($pxFromTop < ($userScreenHeight * 3.2)) {

                   papaWindow.scrollBy(0, ($userScreenHeight / 30));
             }
        }
  else if (cursor.y < (($userScreenHeight + $pxFromTop) * .75)) {

        papaWindow.scrollBy(0, -($userScreenHeight / 30));

        }

   }); //End mouseover()
千纸鹤带着心事 2025-01-08 13:11:24

这不起作用,因为该事件仅在鼠标悬停在 li 上时触发。

('li').mouseover(function(e) { });

当拖动项目时,您需要能够知道鼠标相对于视口的位置。当用户开始拖动项目时,将“mousemove”事件附加到主体,然后在其中检查鼠标位置并在必要时滚动。

$("body").on("mousemove", function(event) {
// Check mouse position - scroll if near bottom or top
});

当用户停止拖动时,不要忘记删除事件。

$("body").off("mousemove", function(event) {
// Check mouse position - scroll if near bottom or top
});

This won't work as the event only fires while you're mouse is over the li.

('li').mouseover(function(e) { });

You need to be able to tell the position of the mouse relative to the viewport when an item is being dragged. When the users starts to drag an item attach an 'mousemove' event to the body and then in that check the mouse position and scroll when necessary.

$("body").on("mousemove", function(event) {
// Check mouse position - scroll if near bottom or top
});

Dont forget to remove your event when the user stops dragging.

$("body").off("mousemove", function(event) {
// Check mouse position - scroll if near bottom or top
});
往日 2025-01-08 13:11:24

这可能不完全是您想要的,但可能会有所帮助。当鼠标位于“屏幕边框”(用户定义的区域)上方时,它将自动滚动。假设屏幕右侧有一个 40px 宽的条,如果鼠标到达第一个 1px,它将开始滚动。你每移动一个像素,速度就会增加。它甚至有一个很好的缓动动画。

http://www.smoothdivscroll.com/v1-2.htm

This may not be exactly what you want, but it might help. It will auto-scroll when the mouse is over the 'border of the screen' (a user defined region). Say you have a 40px wide bar on the right of the screen, if the mouse reaches the first 1px, it will start scrolling. Each px you move into it, the speed will increase. It even has a nice easing animation.

http://www.smoothdivscroll.com/v1-2.htm

似最初 2025-01-08 13:11:24

我从 CodeProject 收到每周时事通讯(电子邮件),其中有一些内容看起来肯定会解决我的问题...希望这可以帮助其他人:

  1. http://johnpolacek.github.com/scrollorama/ -- 基于 JQuery 并为滚动设置动画

  2. < a href="https://github.com/IanLunn/jQuery-Parallax" rel="nofollow">https://github.com/IanLunn/jQuery-Parallax -- 基于 JQuery,类似于上面

  3. http://remysharp。 com/2009/01/26/element-in-view-event-plugin/ -- JQuery,检测元素当前是否在用户视图中(对于这个问题非常有帮助!)

  4. 还有 #2 中的站点有一些有趣的代码:

    var windowHeight = $window.height();
    var navHeight = $('#nav').height() / 2;
    var windowCenter = (windowHeight / 2);
    var newtop = windowCenter - navHeight;
    //ToDo:找到一种方法来使用这些变量和我原来的变量来确定滚动区域
    

I get a weekly newsletter (email) from CodeProject, and it had some stuff that certainly looks like it will solve my problem... hopefully this can help others:

  1. http://johnpolacek.github.com/scrollorama/ -- JQuery based and animates the scroll

  2. https://github.com/IanLunn/jQuery-Parallax -- JQuery based, similar to above

  3. http:// remysharp. com/2009/01/26/element-in-view-event-plugin/ -- JQuery, detects whether an element is currently in view of the user (super helpful for this issue!)

  4. Also the site in #2 had some interesting code:

    var windowHeight = $window.height();
    var navHeight = $('#nav').height() / 2;
    var windowCenter = (windowHeight / 2);
    var newtop = windowCenter - navHeight;
    //ToDo: Find a way to use these vars and my original ones to determine scroll regions
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文