
document.body.attachEvent("onclick", function(){})
document.body.addEventListener("click", function(){}, false)
一、原生js通过window.onscroll监 听
window.onscroll = function() {
//为了保证兼容性,这里取两个值,哪个有值取哪一个
//scrollTop就是触发滚轮事件时滚轮的高度
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
console.log(“滚动距离” + scrollTop)
}
二、Jquery通过$(window).scroll()监听
$(window).scroll(function() {
//为了保证兼容性,这里取两个值,哪个有值取哪一个
//scrollTop就是触发滚轮事件时滚轮的高度
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
console.log(“滚动距离” + scrollTop)
})
将页面滚动到指定位置
主要使用的是锚点技术,锚点元素通过scrollTop值改变进行定位。
锚点
锚点是网页制作中超级链接的一种,又叫命名锚记。命名锚记像一个迅速定位器一样,是一种页面内的超级链接。
法一 直接使用scrollTop
使用scrollTop方法,x为滚轮的高度
$(“body,html”).scrollTop(x)
加动画的滚动
// 滚动到指定位置
$(‘html, body’).animate({
scrollTop: $("#div1").offset().top
}, 2000)
// 滚动到顶部,指定距顶部的距离
$(’.scroll_top’).click(function(){
$(‘html,body’).animate({
scrollTop: ‘0px’
}, 800)
})
方法二 利用hash
hash
hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)。
location.hash=anchorname。
例如:以下地址的hash为#hot
http://this.summer.io/is/#hot // location.hash → #hot
以下函数将页面定位到title元素
function aa(){
window.location.hash = “#title”
}
方法三 直接使用超链接锚点
点击a标签页面将会定位到anchor1元素
方法一:在标签上加入 onLoad="" onResize="" 方法 写上对应的方法即可
方法二:window.onresize=function(){///.....} 在方法里面写上对应的代码即可
<script>function adjust(obj){
var div = document.getElementById("pad")
var txt = document.getElementById("txt")
var w = document.body.clientWidth
var h = document.body.clientHeight
div.style.width = w/3
div.style.height = h/3
txt.style.width = w/6
txt.style.height = h/6
}
window.onload=function(){
window.onresize = adjust
adjust()
}
</script>
<body style="margin:0pxheight:0px">
<div id="pad" style="background:redzoom:1margin:0pxheight:0px">
<input type="text" id="txt">
</div>
</body>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)