如何使用 pixi.js 或 jquery ui 进行自由滚动?

问题描述 投票:0回答:1

我正在尝试制作一个类似于 https://pharrellwilliams.com/ 的页面,其中的自由滚动必须能够导航到左右上下,我不知道它是否是用画布完成的

我可以调查的是它占用了库https://www.pixijs.com还有https://jqueryui.com/

在jquery iu的页面上是这个例子:

 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #draggable { width: 150px; height: 150px; padding: 0.5em; }
  </style>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#draggable" ).draggable();
  } );
  </script>
</head>
<body>

<div id="draggable" class="ui-widget-content">
  <p>Drag me around</p>
</div>

使用此代码,我可以将 div 移动到我想要的位置,但我想要的是像 pharrell williams 的页面一样进行调动,我有一个平等的项目,我想学习

jquery jquery-ui frontend pixi.js
1个回答
0
投票

这是一个可能对您有帮助的示例。它的强度远低于您提供的示例站点。如果这对您有用,您可以将其集成到您自己的项目中。

HTML

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

创建 4 个大 div 部分并将它们专门放置在 HTML 中的基本结构。我们可以将项目放置到页面的负数部分,但我们无法在任一方向将负数滚动到 0,因此最好从 0,0 开始放置它们。

CSS

body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#main {
  position: relative;
}

.part {
  width: 1500px;
  height: 900px;
  position: absolute;
}

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

JavaScript

$(function() {
  function moveToCenter() {
    var vert = $(document).height() - $(window).height();
    var horz = $(document).width() - $(window).width();
    console.log("Centering View:", Math.round(horz / 2), Math.round(vert / 2));
    window.scrollTo(Math.round(horz / 2), Math.round(vert / 2));
  }

  function calcEdges(buff) {
    var t = [0, buff];
    var b = [$(window).height() - buff, $(window).height()];
    var l = [0, buff];
    var r = [$(window).width() - buff, $(window).width()];
    return {
      top: t,
      left: l,
      right: r,
      bottom: b
    };
  }

  function move(d, n) {
    var c, g;
    if (d == "top") {
      c = $(window).scrollTop();
      g = c + n;
      if (g <= 0 || (g >= $(document).height())) {
        console.log("Hit Top/Bottom Boundry");
        return false;
      }
      $(window).scrollTop(g);
    } else {
      c = $(window).scrollLeft();
      g = c + n;
      if (g <= 0 || (g >= $(document).width())) {
        console.log("Hit Left/Right Boundry");
        return false;
      }
      $(window).scrollLeft(g)
    }
  }

  function init() {
    moveToCenter();
    var sides = calcEdges(30);
    $(window).mousemove(function(e) {
      var x = e.clientX,
        y = e.clientY;
      if (x > sides.left[0] && x < sides.left[1]) {
        console.log("Dir: Left", x);
        move("left", -5);
      } else if (x > sides.right[0] && x < sides.right[1]) {
        console.log("Dir: Right", x);
        move("left", 5);
      }
      if (y > sides.top[0] && y < sides.top[1]) {
        console.log("Dir: Top", y);
        move("top", -5);
      } else if (y > sides.bottom[0] && y < sides.bottom[1]) {
        console.log("Dir: Bottom", y);
        move("top", 5);
      }
    });
  }
  init();
});

这里发生了相当多的事情。这是基础知识:

  • 将视口移动到内容中心的函数。这样用户就有了可以滚动到的地方
  • 帮助我们根据距窗口边缘的特定像素数计算“边缘”位置的函数
  • 将窗口向特定方向(
    top
    left
    )移动或滚动特定量的功能
  • 一个初始化所有内容、滚动到中心并设置我们的
    mousemove
    回调事件的函数

工作示例代码:https://jsfiddle.net/Twisty/z8vh6kwx/

工作示例展示:https://jsfiddle.net/Twisty/z8vh6kwx/show

我已经配置了我的示例,当用户将鼠标从屏幕边缘移动 30 像素时,它会沿该方向滚动 5 像素。虽然有点笨拙,但你可以看到它总体上确实有效。您可以有许多不同大小的部件并浮动在页面的不同部分。您可以添加其他悬停样式和单击事件。它不必比这更复杂。

© www.soinside.com 2019 - 2024. All rights reserved.