這次帶給大家如何用jquery做出放大鏡效果,用jquery做出放大鏡效果的注意事項有哪些,下面就是實戰案例,一起來看一下。
jquery寫的兩種放大鏡效果,沒有使用到外掛。調理和思路清晰。不是使用物件導向方式寫的,初學者較容易看懂。廢話不多說,看程式碼。圖片這裡就不上傳了,大家自己找。最好是找到比例的,這樣效果比較好。
<body> <p id="father"> <p id="container"> <img src="/static/imghwm/default1.png" data-src="img/400_1.jpg" class="lazy" style="max-width:90%" alt="如何用jquery做出放大鏡效果" > <img src="/static/imghwm/default1.png" data-src="img/400_2.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" > <p class="shade"></p> </p> <p class="small first"><img src="/static/imghwm/default1.png" data-src="img/50_1.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" ></p> <p class="small second"><img src="/static/imghwm/default1.png" data-src="img/50_2.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" ></p> </p> <p class="big"> <img src="/static/imghwm/default1.png" data-src="img/800_1.jpg" class="lazy" style="max-width:90%" alt="如何用jquery做出放大鏡效果" > <img src="/static/imghwm/default1.png" data-src="img/800_2.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" > </p> </body>
css程式碼
*{padding: 0; margin: 0;} #father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;} #father .second{left: 70px;} .third{left: 140px;} #father{position: relative; top: 100px; left: 50px; height: 460px;} #container{position: absolute; width: 400px; height: 400px;} #container img{position: absolute; display: none;} .shade{width: 200px; height: 200px; position: absolute; background: #000; opacity: 0.4; top: 0; left: 0; display: none;} .big{width: 400px; height: 400px; position: absolute; top: 100px; overflow: hidden; left: 500px; display: none;} .big img{width: 800px; height: 800px; position: absolute; display: none;}
js程式碼
<script type="text/javascript" src='js/jquery-1.12.4.min.js'></script> <script type="text/javascript"> $(function () { changePic('.first',0); changePic('.second',1); var shadeWidth = $('.shade').width(),//阴影的宽度 shadeHeight = $('.shade').height(),//阴影的高度 middleWidth = $('#container').width(),//容器的宽度 middleHeight = $('#container').height(),//容器的高度 bigWidth = $('.big').width(),//放大图片盒子的宽度 bigHeight = $('.big').height(),//放大图片盒子的高度 rateX = bigWidth / shadeWidth,//放大区和遮罩层的宽度比例 rateY = bigHeight / shadeHeight;//放大区和遮罩层的高度比例 //当鼠标移入与移出时阴影与放大去显现/消失 $('#container').hover(function() { $('.shade').show(); $('.big').show(); }, function() { $('.shade').hide(); $('.big').hide(); }).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动 //记录下光标距离页面的距离 var x = e.pageX, y = e.pageY; //设置遮罩层的位置 $('.shade').offset({ top: y-shadeHeight/2, left: x-shadeWidth/2 }); //获取遮罩层相对父元素的位置 var cur = $('.shade').position(), _top = cur.top, _left = cur.left, hdiffer = middleHeight - shadeHeight, wdiffer = middleWidth - shadeWidth; if (_top < 0) _top = 0; else if (_top > hdiffer) _top = hdiffer; if (_left < 0) _left = 0; else if (_left > wdiffer) _left =wdiffer; //判断完成后设置遮罩层的范围 $('.shade').css({ top: _top, left: _left }); //设置放大区图片移动 $('.big img').css({ top: - rateY*_top, left: - rateX*_left }); });; //封装的改变图片显示的函数 function changePic (element,index) { $(element).click(function() { $('#container img').eq(index).css('display', 'block').siblings().css('display', 'none'); $('.big img').eq(index).css('display', 'block').siblings().css('display', 'none'); }); } });
以上是常用的,下面這個是在原始圖基礎上放大的
htm
<body> <p id="father"> <p id="container"> <img src="/static/imghwm/default1.png" data-src="img/400_1.jpg" class="lazy" style="max-width:90%" alt="如何用jquery做出放大鏡效果" > <img src="/static/imghwm/default1.png" data-src="img/400_2.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" > <img src="/static/imghwm/default1.png" data-src="img/400_3.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" > <p class="shade"> <img src="/static/imghwm/default1.png" data-src="img/800_1.jpg" class="lazy" style="max-width:90%" alt="如何用jquery做出放大鏡效果" > <img src="/static/imghwm/default1.png" data-src="img/800_2.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" > <img src="/static/imghwm/default1.png" data-src="img/800_3.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" > </p> </p> <p class="small first"><img src="/static/imghwm/default1.png" data-src="img/50_1.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" ></p> <p class="small second"><img src="/static/imghwm/default1.png" data-src="img/50_2.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" ></p> <p class="small third"><img src="/static/imghwm/default1.png" data-src="img/50_3.jpg" class="lazy" alt="如何用jquery做出放大鏡效果" ></p> </p> </body>
css程式碼
*{padding: 0; margin: 0;} #father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;} #father .second{left: 70px;} .third{left: 140px;} #father{position: relative; top: 100px; left: 50px; height: 460px;} #container{position: absolute; width: 400px; height: 400px;} #container img{position: absolute; display: none;} .shade{width: 200px; height: 200px; position: absolute; top: 0;left: 0; display: none; border-radius: 50%; overflow: hidden; background: #000;} .shade img{display: none; width: 800px; height: 800px; position: absolute;}
js程式碼
#<span style="white-space:pre"> </span><script type="text/javascript" src='js/jquery-1.12.4.min.js'></script> <script type="text/javascript"> $(function () { changePic('.first',0); changePic('.second',1); changePic('.third',2); var shadeWidth = $('.shade').width(),//阴影的宽度 shadeHeight = $('.shade').height(),//阴影的高度 middleWidth = $('#container').width(),//容器的宽度 middleHeight = $('#container').height(),//容器的高度 bigImgWidth = $('.shade img').width(),//放大图片盒子的宽度 bigImgHeight = $('.shade img').height(),//放大图片盒子的高度 rateX = bigImgWidth / middleWidth,//放大区和遮罩层的宽度比例2 rateY = bigImgHeight / middleHeight;//放大区和遮罩层的高度比例2 //当鼠标移入与移出时阴影与放大去显现/消失 $('#container').hover(function() { $('.shade').show(); $('.big').show(); }, function() { $('.shade').hide(); $('.big').hide(); }).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动 //记录下光标距离页面的距离 var x = e.pageX, y = e.pageY; //设置遮罩层的位置 $('.shade').offset({ top: y-shadeHeight/2, left: x-shadeWidth/2 }); //获取遮罩层相对父元素的位置 var cur = $('.shade').position(), _top = cur.top, _left = cur.left, hdiffer = middleHeight - shadeHeight, wdiffer = middleWidth - shadeWidth; if (_top < 0) _top = 0; else if (_top > hdiffer) _top = hdiffer; if (_left < 0) _left = 0; else if (_left > wdiffer) _left =wdiffer; //判断完成后设置遮罩层的范围 $('.shade').css({ top: _top, left: _left }); //设置放大区图片移动 $('.shade img').css({ top: - _top*rateY*3/2, left: - _left*rateX*3/2 }); });; //封装的改变图片显示的函数 function changePic (element,index) { $(element).click(function() { $('#container img').eq(index).css('display', 'block').siblings().css('display', 'none'); $('.shade img').eq(index).css('display', 'block').siblings().css('display', 'none'); }); } }); <span style="white-space:pre"> </span></script>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
以上是如何用jquery做出放大鏡效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver CS6
視覺化網頁開發工具