平滑滚动前的睡眠功能
Sleep function before smooth scroll
如何在平滑滚动之前添加3秒的暂停?用户将点击按钮,然后会有3秒的睡眠,然后会运行平滑滚动。
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^'//,'') == this.pathname.replace(/^'//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
您可以添加一个setTimeout(),如下所示:
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^'//,'') == this.pathname.replace(/^'//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
setTimeout(function(){
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
}, 3000);
return false;
}
}
});
});
您可以使用JavaScript标准的setTimeout()函数:
JavaScript
setTimeout(function () {
// function that is executed after the timer ends
}, 3000);
正如您所看到的,setTimeout
函数有两个参数:一个是将在计时器结束后执行的处理程序(函数),另一个是以毫秒为单位定义计时器持续时间的整数。
如果您不熟悉所有这些"处理"概念,请考虑下面的例子,我们也这样做,但首先将函数"保存"在变量中:
JavaScript
var fnCallback = function () {
console.log('This plague works.');
};
// Call setTimeout() with a handler function (fnCallback), and an integer (3000)
setTimeout(fnCallback, 3000);
相关文章:
- jQuery滚动功能只工作一次
- 向表单添加滚动功能
- jQuery触摸滑动内容滚动功能
- 滚动功能不工作
- 网络驱动程序中使用的向下滚动功能从网页的哪个部分进行搜索
- jQuery Div滚动功能:IE中的问题
- 单击水平滚动库中的图像以居中显示无法工作的功能
- 如何使用位置获取滚动功能获取 HTML 中的元素标签
- 在iPad Web App上拖放 - 同时保留滚动功能
- 如何在PDF中放置滚动功能.js
- 如何在幻灯片中添加向上滚动功能单击1次即可向下滚动
- jquery滚动功能无法正常工作
- 当滚动到指定的位置时,启动功能,如何重置它并重新启动,如果我们再次回到这个位置
- 删除滚动条,但保留滚动功能
- 滚动功能不工作
- 锚定标签功能强大,但滚动流畅赢得't工作;页面的行为就像javascript是'没有
- 滚动功能按相反顺序工作
- 滚动功能工作方向错误
- Php聊天功能.滚动到'帖子区域'
- 窗口加载&功能滚动页不起作用