【发布时间】:2014-07-02 19:30:17
【问题描述】:
我使用了 2 个在 Internet 上找到的脚本,一个用于平滑滚动到页面底部的 DIV,另一个用于平滑滚动“返回顶部”。问题是两者之间存在冲突,因此“返回顶部”不起作用(点击时没有返回顶部)。单独使用它们都可以完美地工作。
如何将它们“合并”到一个脚本中? (并保留back to top脚本的淡入淡出效果)见http://jsfiddle.net/GjsVq/1/
非常感谢
$(document).ready(function() {
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
$(document).ready(function() {
var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});
jQuery('.back-to-top').click(function(event) {
event.preventDefault();
jQuery('html, body').animate({scrollTop: 0}, duration);
return false;
})
});
【问题讨论】:
标签: javascript jquery html css