日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery 判断元素是否在屏幕可见区域内

發布時間:2024/9/19 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery 判断元素是否在屏幕可见区域内 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

  • 人眼可見區域(document.body.clientWidth,document.body.clientHeight)
  • 網頁可見區域(document.documentElement.clientWidth,document.documentElement.clientHeight)

判斷元素是否在網頁可見區域內

$.fn.isOnHtmlScreen = function(){var win = $(window);var viewport = {top : 0,left : 0};viewport.right = viewport.left + win.width();viewport.bottom = viewport.top + win.height();var bounds = this.offset();bounds.right = bounds.left + this.outerWidth();bounds.bottom = bounds.top + this.outerHeight();return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom)); };

判斷元素是否在人眼可見區域內

$.fn.isOnVisibleScreen = function(){var win = $(window);var viewport = {top : win.scrollTop(),left : win.scrollLeft()};viewport.right = viewport.left + document.body.clientWidth;viewport.bottom = viewport.top + document.body.clientHeight;var bounds = this.offset();bounds.right = bounds.left + this.width();bounds.bottom = bounds.top + this.height();return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom)); };

isOnVisibleScreen 示例

<html> <header> <script crossorigin="anonymous" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" src="https://lib.baomitu.com/jquery/3.5.1/jquery.min.js"></script> <script type="text/javascript"> $.fn.isOnVisibleScreen = function(){var win = $(window);var viewport = {top : win.scrollTop(),left : win.scrollLeft()};viewport.right = viewport.left + document.body.clientWidth;viewport.bottom = viewport.top + document.body.clientHeight;var bounds = this.offset();bounds.right = bounds.left + this.width();bounds.bottom = bounds.top + this.height();return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));}; function changeBtn() {if (!$('.item:eq(0)').isOnVisibleScreen()) {$(".rightFlatBar div:eq(0)").fadeIn(1000);$(".rightFlatBar div:eq(1)").fadeIn(1000);} else {$(".rightFlatBar div:eq(0)").fadeOut(1000);$(".rightFlatBar div:eq(1)").fadeOut(1000);}if (!$('.item:eq(1)').isOnVisibleScreen()) {$(".rightFlatBar div:eq(2)").fadeIn(1000);} else {$(".rightFlatBar div:eq(2)").fadeOut(1000);}if (!$('.item:eq(2)').isOnVisibleScreen()) {$(".rightFlatBar div:eq(3)").fadeIn(1000);} else {$(".rightFlatBar div:eq(3)").fadeOut(1000);}if (!$('.item:eq(3)').isOnVisibleScreen()) {$(".rightFlatBar div:eq(4)").fadeIn(1000);} else {$(".rightFlatBar div:eq(4)").fadeOut(1000);}if (!$('.item:eq(4)').isOnVisibleScreen()) {$(".rightFlatBar div:eq(5)").fadeIn(1000);} else {$(".rightFlatBar div:eq(5)").fadeOut(1000);} } $(function(){changeBtn();$(window).scroll(changeBtn);$(".rightFlatBar div:eq(0)").click(function(){$('html,body').scrollTop(0);});$(".rightFlatBar div:eq(1)").click(function(){$('html,body').animate({scrollTop:$('.item:eq(0)').offset().top}, 1000);});$(".rightFlatBar div:eq(2)").click(function(){$('html,body').animate({scrollTop:$('.item:eq(1)').offset().top - 30}, 1000);});$(".rightFlatBar div:eq(3)").click(function(){$('html,body').animate({scrollTop:$('.item:eq(2)').offset().top - 30}, 1000);});$(".rightFlatBar div:eq(4)").click(function(){$('html,body').animate({scrollTop:$('.item:eq(3)').offset().top}, 1000);});$(".rightFlatBar div:eq(5)").click(function(){$('html,body').animate({scrollTop:$('.item:eq(4)').offset().top}, 1000);}); }); </script> <style type="text/css"> *{margin:0;} .item{width: 100%;height: 300px;padding: 100px 0 0 100px; } .item:nth-of-type(even){background: #abc;color: #fff; } .item:nth-of-type(odd){background: #fff;color: #333; } .rightFlatBar {position: fixed;right: 5px;bottom: 0px;width: 60px;height: auto; } .rightFlatBar div{display: none;width: 60px;height: 60px;margin-bottom: 5px;border: 1px solid #ececec;border-radius: 5px;transition: .3s;box-shadow: 0px 0px 3px rgba(0,0,0,0.08);text-align:center;line-height: 60px;background: #fff;cursor:hand; }</style> </header><body><div><div class="item"><h1>條目1</h1></div><div class="item"><h1>條目2</h1></div><div class="item"><h1>條目3</h1></div><div class="item"><h1>條目4</h1></div><div class="item"><h1>條目5</h1></div></div><div class="rightFlatBar"><div>頂部</div><div>條目1</div><div>條目2</div><div>條目3</div><div>條目4</div><div>條目5</div></div> </body> </html>

參考

https://www.xuebuyuan.com/171078.html
https://www.cnblogs.com/whb17bcdq/p/6513766.html

可見區域示例

<html> <header> <script crossorigin="anonymous" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" src="https://lib.baomitu.com/jquery/3.5.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){console.log("w={},h={}", document.body.clientWidth, document.body.clientHeight);console.log("w={},h={}", $(document.body)[0].clientWidth, $(document.body)[0].clientHeight);console.log("w={},h={}", document.documentElement.clientWidth, document.documentElement.clientHeight);console.log("w={},h={}", window.screen.width, window.screen.height);console.log("w={},h={}", window.screen.availWidth, window.screen.availHeight);console.log("w={},h={}", $(window).width(), $(window).height());console.log("w={},h={}", $(document).width(), $(document).height());console.log("w={},h={}", $(document.body).width(), $(document.body).height()); }); </script> <style type="text/css"> *{margin:0;} .item{width: 100%;height: 300px;padding: 100px 0 0 100px; } .item:nth-of-type(even){background: #abc;color: #fff; } .item:nth-of-type(odd){background: #fff;color: #333; } .rightFlatBar {position: fixed;right: 5px;bottom: 0px;width: 60px;height: auto; } .rightFlatBar span{display: block;width: 60px;height: 60px;margin-bottom: 5px;border: 1px solid #ececec;border-radius: 5px;transition: .3s;box-shadow: 0px 0px 3px rgba(0,0,0,0.08);text-align:center;line-height: 60px;background: #fff;cursor:hand; } </style> </header><body><div><div class="item"><h1>條目1</h1></div><div class="item"><h1>條目2</h1></div><div class="item"><h1>條目3</h1></div><div class="item"><h1>條目4</h1></div><div class="item"><h1>條目5</h1></div></div><div class="rightFlatBar"><span>頂部</span><span>條目1</span><span>條目2</span><span>條目3</span><span>條目4</span><span>條目5</span></div> </body> </html>

運行效果:

總結

以上是生活随笔為你收集整理的jQuery 判断元素是否在屏幕可见区域内的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。