ios - Parallax:在网页中获取视差事件

标签 ios web-applications parallax

当网页提供给 iPhone 时,如何在网页上的 javascript 中接收视差事件?我查看了 github.com 的 404 页面,它正在使用加速度计响应运动。只需尝试访问 github.com/f7fu3f73fh39f8h3f93fh398h(或任何编造的页面)并查看他们正在制作的酷炫视差动画。

我查看了源代码,并没有看到他们为获取事件所做的任何特殊操作。

最佳答案

我认为他们只是在使用 Plax

它出现在 plax example page加速度计的东西是由那个插件处理的。

他们只在该页面中包含 jquery 和 plax

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="js/plax.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $('#plax-sphere-1').plaxify({"xRange":40,"yRange":40})
    $('#plax-logo').plaxify({"xRange":20,"yRange":20})
    $('#plax-sphere-2').plaxify({"xRange":10,"yRange":10})
    $('#plax-sphere-3').plaxify({"xRange":40,"yRange":40,"invert":true})
    $.plax.enable()
  })
</script>

然后在 plax.js lines 209-243
// Determine if the device has an accelerometer
//
// returns true if the browser has window.DeviceMotionEvent (mobile)
function moveable(){
  return (ignoreMoveable===true) ? false : window.DeviceOrientationEvent !== undefined;
}

// The values pulled from the gyroscope of a motion device.
//
// Returns an object literal with x and y as options.
function valuesFromMotion(e) {
  x = e.gamma;
  y = e.beta;

  // Swap x and y in Landscape orientation
  if (Math.abs(window.orientation) === 90) {
    var a = x;
    x = y;
    y = a;
  }

  // Invert x and y in upsidedown orientations
  if (window.orientation < 0) {
    x = -x;
    y = -y;
  }

  motionStartX = (motionStartX === null) ? x : motionStartX;
  motionStartY = (motionStartY === null) ? y : motionStartY;

  return {
    x: x - motionStartX,
    y: y - motionStartY
  };
}

关于ios - Parallax:在网页中获取视差事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23958909/

相关文章:

javascript - 如何使用 JavaScript 跳转到放置在页面底部的特定 div?

reactjs - 如何在 React 中使用 Relax.js?

java - SetTwo 类路径 用于执行 Java 程序

java - 是否可以抛出 java.lang.Error?

ios - 数据未显示在 swift 中的自定义 tableview 单元格中,添加了下面的单元格类

ios - 在我的 podspec 中设置 DEFINES_MODULE = YES 有什么缺点吗?

android - 绘制大型位图时 nSyncAndDrawFrame 速度极慢

html - 为什么视差不能使用 html css

ios - 当我使用 AVPlayerLayer 时放大镜以错误的方式出现

ios - 如何将导航栏添加到带有选项卡栏项的 View Controller