js滚动监听
滾動(dòng)監(jiān)聽語句:
1、當(dāng)前滾動(dòng)的地方的窗口頂端到整個(gè)頁面頂端的距離:
var winPos = $(window).scrollTop();2、獲取指定元素的頁面位置:? ? ?
$(val).offset().top;3、對(duì)頁面滾動(dòng)條滾動(dòng)的監(jiān)聽:要放在頁面加載的時(shí)候
$(window).scroll(function(event){});4、設(shè)置滾動(dòng)條到指定位置。
$(window).scrollTop(offset)。5、獲取水平滾動(dòng)條的距離
$(document).scrollLeft();6、獲取整個(gè)頁面高度
$(document).height();7、獲取當(dāng)前瀏覽器,也就是你的瀏覽器所看到的頁面部分的高度,會(huì)隨著頁面放縮改變
$(window).height();?
$(window).height()代表了當(dāng)前可見區(qū)域的大小,而$(document).height()則代表了整個(gè)文檔的高度,可視具體情況使用.
注意當(dāng)瀏覽器窗口大小改變時(shí)(如最大化或拉大窗口后) $(window).height() 隨之改變,但是$(document).height()是不變的。
$(function (){ /*滾動(dòng)監(jiān)聽 獲取滾動(dòng)條距頁面頂端距離*/$(window).scroll(function(){ /*定義滾動(dòng)條距頂端距離*/var ww=$(document).scrollTop(); /*根據(jù)距離判斷,改變樣式*/if(ww < 1200 && ww>0 ){ $("#mm1").css("background","#108EE9");$("#mm1").children('.j_p').css("color","#FFF");$("#mm1").find('span').css("color","#FFF");$("#mm2").css("background","#FFF");$("#mm2").children('.j_p').css("color","#108EE9");$("#mm2").find('span').css("color","#108EE9");}else if(ww<2000 && ww>=1200 ){$("#mm1").css("background","#FFF");$("#mm1").children('.j_p').css("color","#108EE9");$("#mm1").find('span').css("color","#108EE9"); $("#mm2").css("background","#108EE9");$("#mm2").children('.j_p').css("color","#FFF");$("#mm2").find('span').css("color","#FFF");}});錨點(diǎn)定位:
用a標(biāo)簽,href=“#id”,鏈接要定位的id屬性
<a href="#id"> <div>跳轉(zhuǎn)</div> </a>
鼠標(biāo)滾輪:mousewheel
這個(gè)事件在標(biāo)準(zhǔn)下和IE下是有區(qū)別的。
firefox是按標(biāo)準(zhǔn)實(shí)現(xiàn)的,事件名為"DOMMouseScroll?",IE下采用的則是"mousewheel?"。
當(dāng)然一行代碼就解決了兼容問題?var mousewheel = document.all?"mousewheel":"DOMMouseScroll"; 事件屬性,IE是event.wheelDelta,Firefox是event.detail?屬性的方向值也不一樣, IE向上滾 > 0,Firefox向下滾 > 0。 使用mousewheel事件有以下兩種方式: 使用mousewheel和unmousewheel事件函數(shù); 使用經(jīng)典的bind和unbind函數(shù)。 $('div.mousewheel_example').mousewheel(fn); $('div.mousewheel_example').bind('mousewheel', fn); mousewheel事件的處理函數(shù)有一點(diǎn)小小的變化,它除了第一個(gè)參數(shù)event外,還接收到第二個(gè)參數(shù)delta。通過參數(shù)delta可以獲取鼠標(biāo)滾輪的方向和速度。 如果delta的值是負(fù)的,那么滾輪就是向下滾動(dòng),正的就是向上。 // 綁定方式 $('#my_elem').bind('mousewheel', function(event, delta) { console.log(delta); }); // 事件函數(shù)方式 $('#my_elem').mousewheel(function(event, delta) { console.log(delta); });
?
轉(zhuǎn)載于:https://www.cnblogs.com/dk2557/p/9274315.html
總結(jié)
- 上一篇: SoapUI简介和入门实例解析
- 下一篇: 里式转换