[Jquery] js擷取瀏覽器捲軸距離頂端的距離

來源:互聯網
上載者:User

標籤:style   blog   color   java   使用   os   io   strong   

需要用js擷取捲軸距離視窗頂端的距離和js擷取瀏覽器可視化視窗的大小

 一、jQuery擷取的相關方法

 

 

jquery 擷取捲軸高度擷取瀏覽器顯示地區的高度 : $(window).height(); 擷取瀏覽器顯示地區的寬度 :$(window).width(); 擷取頁面的文檔高度 :$(document).height(); 擷取頁面的文檔寬度 :$(document).width();擷取捲軸到頂部的垂直高度 :$(document).scrollTop(); 擷取捲軸到左邊的垂直寬度 :$(document).scrollLeft();計算元素位置和位移量:$(id).offset();offset方法是一個很有用的方法,它返回封裝集中第一個元素的位移資訊。預設情況下是相對body的位移資訊。結果包含 top和left兩個屬性。offset(options, results)options.relativeTo  指定相對計算位移位置的祖先元素。這個元素應該是relative或absolute定位。省略則相對body。options.scroll  是否把捲軸計算在內,預設TRUEoptions.padding  是否把padding計算在內,預設falseoptions.margin  是否把margin計算在內,預設trueoptions.border  是否把邊框計算在內,預設true 

 但是我在使用jQuery的方法的時候在IE6上會發生不相容現象。

  二、使用js擷取的相關方法

 

//回到頁面頂部        $("#goTotop").click(function(){                $(‘body,html‘).animate({scrollTop:0},1500); //點擊按鈕讓其回到頁面頂部        });                $(window).scroll(function() {                var yheight1=window.pageYOffset; //捲軸距頂端的距離                var yheight=getScrollTop(); //捲軸距頂端的距離                var height =document.documentElement.clientHeight//瀏覽器可視化視窗的大小                var top=parseInt(yheight)+parseInt(height)-217;                var divobj=$(".kf");                divobj.attr(‘style‘,‘top:‘+top+‘px;‘);        })        /** * 擷取捲軸距離頂端的距離 * @return {}支援IE6 */function getScrollTop() {                var scrollPos;                if (window.pageYOffset) {                scrollPos = window.pageYOffset; }                else if (document.compatMode && document.compatMode != ‘BackCompat‘)                { scrollPos = document.documentElement.scrollTop; }                else if (document.body) { scrollPos = document.body.scrollTop; }                 return scrollPos; }

 getScrollTop()使用這個方法在IE、Google和Firefox上都能擷取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.