jQuery 控制網頁捲軸移動 & Ignore link '#' method jump action
2024-09-19 22:51:00
$('a.gotoheader').click(function(){
// 讓捲軸移動到 0 的位置
$('html, body').scrollTop();
// ignore link "#" method
return false;
});
//在scroll時加入動畫效果
$('a.gotoheader').click(function(){
// 修正 Opera 問題
var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
$body.animate({
scrollTop:
}, ); return false;
});
//scroll到頁面指定的位置,最後再加上easeOutBounce 的動畫效果
$('a.gotoheader').click(function(){
// 修正 Opera 問題
var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
$body.animate({
scrollTop: $('#header').offset().top
}, , 'easeOutBounce'); return false;
});
想要ignore link "#" method, 當click一個link時,page不要跳轉,可使用
<a href="javascript:void(0);">Example</a>
來代替
<a href="#">Example</a>
也可以使用上面例子中提到在,onclick事件觸發後的function中 return false;
另外一種方法就是使用 event.preventDefault(); 取消事件的預設行為
<!DOCTYPE html>
<html>
<head>
<title>preventDefault example</title>
</head> <body>
<p>Please click on the checkbox control.</p>
<form>
<label for="id-checkbox">Checkbox</label>
<input type="checkbox" id="id-checkbox"/>
</form>
<script>
document.querySelector("#id-checkbox").addEventListener("click", function(event){
alert("preventDefault will stop you from checking this checkbox!")
event.preventDefault();
}, false);
</script>
</body>
</html>
最新文章
- [转](二)unity4.6Ugui中文教程文档-------概要-UGUI Canvas
- UnityException: Texture is not readable
- DOM笔记(九):引用类型、基本包装类型和单体内置对象
- 当调用List Remove 失效时 [C#] .
- 使用get方式提交数据
- 教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是
- 使用AndroidScreenSlidePager开源库
- node http.request请求
- [转] ubuntu 12.04 安装 nginx+php+mysql web服务器
- 给Eclipse安装Google app engine插件
- Linux下tomcat的安装详解
- BOM API
- Java框架概述
- 记我的第二次自动化尝试——selenium+pageobject+pagefactory实现自动化下单、退款、撤销回归测试
- [Swift]LeetCode715. Range 模块 | Range Module
- Scala(四) —— 集合
- JS操作字符串
- php重定向http请求
- select函数用法详解
- MySQL基础(二):视图、触发器、函数、事务、存储过程