【问题标题】:Css - Mobile posistion fixed , definitive solutionCss - 移动位置固定,明确的解决方案
【发布时间】:2013-04-28 10:01:05
【问题描述】:

我在移动设备上遇到了问题

<div class="sticky">sticky to bottom</div>

.sticky{
position:fixed;
bottom:0;
right:0;
left:0;
width:100%;
}

在桌面上显然很好,在移动设备上(我只在 android 上测试过,但我猜也在 iOS 上),在滚动页面时,它不会一直粘在窗口底部,而是向上滚动与文档一起。

于是,我通过stack和google搜索,找到了很多解决方案,iScroll.js、Scrollability、webkit tricks等等。

有没有人可以在性能方面给我更好的解决方案(我不想在我的页面中包含大文件)?

谢谢。

实际上我正在尝试使用此代码:

$(window).on('scrollstart',function(){
    $('.sticky').css('top','100%').fadeOut();
  });
  $(window).on('scrollstop',function(){
    $('.sticky').css('top','100%').fadeIn();
  });

(function(){

  var special = jQuery.event.special,
  uid1 = 'D' + (+new Date()),
  uid2 = 'D' + (+new Date() + 1);

  special.scrollstart = {
    setup: function() {

      var timer,
      handler =  function(evt) {

        var _self = this,
        _args = arguments;

        if (timer) {
          clearTimeout(timer);
        } else {
          evt.type = 'scrollstart';
          jQuery.event.handle.apply(_self, _args);
        }

        timer = setTimeout( function(){
          timer = null;
        }, special.scrollstop.latency);

      };

      jQuery(this).bind('scroll', handler).data(uid1, handler);

    },
    teardown: function(){
      jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
    }
  };

  special.scrollstop = {
    latency: 300,
    setup: function() {

      var timer,
      handler = function(evt) {

        var _self = this,
        _args = arguments;

        if (timer) {
          clearTimeout(timer);
        }

        timer = setTimeout( function(){

          timer = null;
          evt.type = 'scrollstop';
          jQuery.event.handle.apply(_self, _args);

        }, special.scrollstop.latency);

      };

      jQuery(this).bind('scroll', handler).data(uid2, handler);

    },
    teardown: function() {
      jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
    }
  };

})();

【问题讨论】:

  • 这是由于浏览器在手指按下时未发送滚动事件造成的,如果没有大型 javascript 库 AFAIK,您将无能为力。

标签: javascript html css mobile css-position


【解决方案1】:

我已经遇到过这个问题,就像你的许多解决方案对我不好,然后我使用这种技术来让用户看起来好:

如果您滚动,则使用 Javascript 检测,然后隐藏该块,直到滚动停止并再次显示。

【讨论】:

  • 我正在测试它,但是滚动呢,我红色的触摸设备直到它停止才会触发滚动:/!?
  • 你可以使用这个事件:scrollstart和scrollstop来检测滚动是否停止:)
  • 哦,这很好,我会告诉你这个,如果没有其他好的答案我会接受这个(如果它有效,呵呵)
  • @katch 检查我更新了我的问题,问题仍然存在,我尝试像你告诉我的那样但是当滚动停止页面返回顶部显示粘性元素并且它不会保持在相同的窗口 y 位置,难以置信的大声笑
  • 您必须使用 .hide() 和 .show() 进行更改,而不是使用 fadeOut() 和 fadeIn(),因为这需要时间;)
猜你喜欢
  • 2020-11-04
  • 1970-01-01
  • 2013-10-29
  • 2014-11-29
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
相关资源
最近更新 更多