javascript - 如何为 iOS 和平板电脑设置 $.Scroll?

标签 javascript jquery ios cross-browser tablet

我正在尝试使用 jQuery/JavaScript 设置滚动条。我无法弄清楚如何使用 iOS 和平板电脑的视口(viewport)。任何帮助将不胜感激。我需要将 $.Scroll 用于设计/动画目的。

谢谢,

这是我目前所拥有的:


  

    var isIphone = navigator.userAgent.match(/iPhone/i) != null;
    var isIpod = navigator.userAgent.match(/iPod/i) != null;
    var isIpad = navigator.userAgent.match(/iPad/i) != null;

    // now set one variable for all iOS devices
    // What do I do for Tablet PC's?

    var isIos = isIphone || isIpod || isIpad;

    jQuery(function ($) {
      $.Window = $(window); // We'll use this later on 
      $.Body = $('body');
      if ( !isIos ) {
        // Ternary for desktop
        $.Scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $.Body;
      } else {
        // Need to detect iOS and Tablet PC's
        $.Scroll = $.Body;
      }
    });

  

更新:

谢谢,到目前为止的输入!

我已经弄清楚如何访问 iOS 触摸事件:


  

    $.Window
      .bind('touchmove',
        function (e) {
          //code goes here
      })
      .bind('scroll',
        function (e) {
          //code goes here
      })

  

这似乎适用于滚动和触摸事件。

最佳答案

我认为 window.scrollTo(x, y) 会得到你想要的,但你需要确保窗口实际上是可滚动的(即内容大于窗口并且正文溢出是可滚动的)。

旁注:我还没有见过有人像这样在 jQuery 库中存储变量。为什么不直接做 var scrollableObject = ($.browser.mozilla || $.browser.msie) 呢? $('html') : $.正文;?

关于javascript - 如何为 iOS 和平板电脑设置 $.Scroll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9774530/

相关文章:

javascript - addEventListener、箭头函数和 `this`

php - Chrome 在 PHP 脚本执行期间停止读取 JSON 文件

javascript - 从 AJAX 下载 PDF 文件成功回调

jquery - 在 :fixed tablets only 位置发出

ios - NSBatchUpdateRequest没有保存Transformable属性

javascript - 如何将键盘按钮 "Return"更改为 "Search"以在 UIWebView 中输入?

javascript - Weather App React 只需点击三下即可更新状态并传递 Prop

javascript - jQuery 验证插件 - 焦点事件与键/鼠标事件不一致

jquery - 当添加权限为 False 时,Django-grappelli 添加按钮在内联中可见

ios - 如何在后台每 5 秒重复一次本地通知