如何使用pixi.js或jQuery UI实现自由滚动?

sqougxex  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(227)

我试图使一个页面类似于https://pharrellwilliams.com/的自由滚动,必须能够导航到左,右,下,上,我不知道如果它是用画布做的
我可以调查的是,它占用了https://www.pixijs.com库,也占用了https://jqueryui.com/
在jquery iu的页面上是这个例子:

  1. <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  2. <link rel="stylesheet" href="/resources/demos/style.css">
  3. <style>
  4. #draggable { width: 150px; height: 150px; padding: 0.5em; }
  5. </style>
  6. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  7. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  8. <script>
  9. $( function() {
  10. $( "#draggable" ).draggable();
  11. } );
  12. </script>
  13. </head>
  14. <body>
  15. <div id="draggable" class="ui-widget-content">
  16. <p>Drag me around</p>
  17. </div>

字符串
有了这段代码,我可以把div移动到我想要的地方,但我想要的是移动,就像在phillill威廉姆斯的页面中一样,我有一个平等的项目,我想学习

wko9yo5t

wko9yo5t1#

这里有一个例子可能对你有帮助。它远没有你提供的例子网站那么密集。如果这对你有用,你可以把它集成到你自己的项目中。

HTML格式

  1. <div id="main">
  2. <div class="part" style="background-color: rgba(255,0,0,0.25); top: 0; left:0;"></div>
  3. <div class="part" style="background-color: rgba(0,0,255,0.25); top: 0; left: 1500px;"></div>
  4. <div class="part" style="background-color: rgba(255,255,0,0.25); top: 900px; left: 0;"></div>
  5. <div class="part" style="background-color: rgba(0,255,0,0.25); top: 900px; left: 1500px;"></div>
  6. </div>

字符串
基本结构,创建4个大的div部分,并专门将它们放置在HTML中。我们可以将项目放置在页面的负部分,但我们不能在任何方向上将负滚动到0,所以最好将它们从0,0放置。

CSS

  1. body {
  2. margin: 0;
  3. padding: 0;
  4. overflow: hidden;
  5. }
  6. #main {
  7. position: relative;
  8. }
  9. .part {
  10. width: 1500px;
  11. height: 900px;
  12. position: absolute;
  13. }


这有助于我们隐藏滚动条和设置其他常规样式。

JavaScript

  1. $(function() {
  2. function moveToCenter() {
  3. var vert = $(document).height() - $(window).height();
  4. var horz = $(document).width() - $(window).width();
  5. console.log("Centering View:", Math.round(horz / 2), Math.round(vert / 2));
  6. window.scrollTo(Math.round(horz / 2), Math.round(vert / 2));
  7. }
  8. function calcEdges(buff) {
  9. var t = [0, buff];
  10. var b = [$(window).height() - buff, $(window).height()];
  11. var l = [0, buff];
  12. var r = [$(window).width() - buff, $(window).width()];
  13. return {
  14. top: t,
  15. left: l,
  16. right: r,
  17. bottom: b
  18. };
  19. }
  20. function move(d, n) {
  21. var c, g;
  22. if (d == "top") {
  23. c = $(window).scrollTop();
  24. g = c + n;
  25. if (g <= 0 || (g >= $(document).height())) {
  26. console.log("Hit Top/Bottom Boundry");
  27. return false;
  28. }
  29. $(window).scrollTop(g);
  30. } else {
  31. c = $(window).scrollLeft();
  32. g = c + n;
  33. if (g <= 0 || (g >= $(document).width())) {
  34. console.log("Hit Left/Right Boundry");
  35. return false;
  36. }
  37. $(window).scrollLeft(g)
  38. }
  39. }
  40. function init() {
  41. moveToCenter();
  42. var sides = calcEdges(30);
  43. $(window).mousemove(function(e) {
  44. var x = e.clientX,
  45. y = e.clientY;
  46. if (x > sides.left[0] && x < sides.left[1]) {
  47. console.log("Dir: Left", x);
  48. move("left", -5);
  49. } else if (x > sides.right[0] && x < sides.right[1]) {
  50. console.log("Dir: Right", x);
  51. move("left", 5);
  52. }
  53. if (y > sides.top[0] && y < sides.top[1]) {
  54. console.log("Dir: Top", y);
  55. move("top", -5);
  56. } else if (y > sides.bottom[0] && y < sides.bottom[1]) {
  57. console.log("Dir: Bottom", y);
  58. move("top", 5);
  59. }
  60. });
  61. }
  62. init();
  63. });


这里有相当多的东西。这里是基本的:

  • 一个将视口移动到内容中心的功能。这样用户就有地方可以滚动到
  • 一个帮助我们计算“边缘”位置的函数,该函数基于从窗口边缘开始的特定像素数
  • 在特定方向(topleft)移动或滚动窗口特定量的功能
  • 一个初始化一切的函数,滚动到中心并设置我们的mousemove回调事件

工作示例代码:https://jsfiddle.net/Twisty/z8vh6kwx/
工作示例显示:https://jsfiddle.net/Twisty/z8vh6kwx/show
我已经配置了我的例子,当用户将鼠标从屏幕边缘移动30像素时,它会向那个方向滚动5像素。这有点笨拙,但你可以看到它确实工作。你可以有许多不同大小的部件,并浮动在页面的不同部分。你可以添加其他悬停样式和单击事件。它不必比这更复杂。

展开查看全部

相关问题