javascript - jquery首页电梯门效果 chorme下有效果,ff和ie没效果。
巴扎黑
巴扎黑 2017-04-10 16:39:16
[JavaScript讨论组]
//首页电梯门效果
$('#fast-nav').find('li').on('click', function (e) {
  var $this = $(this),
  $fast_nav = $('#fast-nav');
  e.preventDefault();
  // 获取当前active项的位置
  var index = $fast_nav.find('.active').index();
  // 获取点击目标的位置
  var tar_index = $this.index();
  var id = $this.data('href'),
  height = $(id).offset().top;
  if (index > tar_index) {
    $('body').animate({
      scrollTop: height - 1
    }, 500);
  } else {
    $('body').animate({
      scrollTop: height + 1
    }, 500);
  }
});

获取点击目标的位置滚动到相应的html类。
本人新手,不知道ff和ie为什么不兼容的原因?

巴扎黑
巴扎黑

全部回复(3)
ringa_lee

因为 IE 6+ (还是 7+? 记不清楚了…), Edge 12, Firefox 等浏览器的最外层 scrollTop 默认在 html 元素,而 IE <= 5, Edge >=13, 以及 Safari, Chrome 和其他 WebKit 内核的浏览器的最外层 scrollTop 默认在 body 元素。(当然 Edge 和 Chrome 中可以在 about:config 把最外层滚动属性改成符合 W3C 规范的 html 元素)

一个兼容性比较好的做法是

// ...
if (index > tar_index) {
    $('html,body').animate({ // browser compatibility
        scrollTop: height - 1
    }, 500);
} else {
    $('html,body').animate({
        scrollTop: height + 1
    }, 500);
}
阿神

~’‘’scrollTop‘’‘这个属性可以使用么?~

可以的~汗一个,API看的还是不够哦~~~

In addition to style properties, some non-style properties such as scrollTop and scrollLeft, as well as custom properties, can be animated.

巴扎黑

电梯门效果是啥?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号