欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

玩转JQuery来实现放大镜效果

程序员文章站 2022-06-19 15:28:19
目前流行的放大镜效果,在很多企业网站以及一些商城网站被广泛使用,下面我们一起看看如何来实现它吧,精彩继续,敬请期待~ Document...</div> <div class="content"> <p>目前流行的放大镜效果,在很多企业网站以及一些商城网站被广泛使用,下面我们一起看看如何来实现它吧,精彩继续,敬请期待~</p> <pre><code class="prism language-javascript"><span class="token operator"><</span><span class="token operator">!</span><span class="token constant">DOCTYPE</span> html<span class="token operator">></span> <span class="token operator"><</span>html lang<span class="token operator">=</span><span class="token string">"en"</span><span class="token operator">></span> <span class="token operator"><</span>head<span class="token operator">></span> <span class="token operator"><</span>meta charset<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span> <span class="token operator"><</span>meta name<span class="token operator">=</span><span class="token string">"viewport"</span> content<span class="token operator">=</span><span class="token string">"width=device-width, initial-scale=1.0"</span><span class="token operator">></span> <span class="token operator"><</span>title<span class="token operator">></span>Document<span class="token operator"><</span><span class="token operator">/</span>title<span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"./jquery.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>style<span class="token operator">></span> <span class="token operator">*</span><span class="token punctuation">{</span> margin<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> padding<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> #small<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">500</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">500</span>px<span class="token punctuation">;</span> border<span class="token punctuation">:</span> <span class="token number">1</span>px red solid<span class="token punctuation">;</span> position<span class="token punctuation">:</span> relative<span class="token punctuation">;</span> <span class="token punctuation">}</span> #small img<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">500</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">500</span>px<span class="token punctuation">;</span> <span class="token punctuation">}</span> #big<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">500</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">500</span>px<span class="token punctuation">;</span> border<span class="token punctuation">:</span> <span class="token number">1</span>px red solid<span class="token punctuation">;</span> <span class="token comment">/* 滚动条自动溢出隐藏 */</span> <span class="token comment">/* overflow: auto; */</span> overflow<span class="token punctuation">:</span> hidden<span class="token punctuation">;</span> margin<span class="token operator">-</span>left<span class="token punctuation">:</span> <span class="token number">10</span>px<span class="token punctuation">;</span> <span class="token punctuation">}</span> #big img<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">1000</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">1000</span>px<span class="token punctuation">;</span> <span class="token punctuation">}</span> #box<span class="token punctuation">{</span> display<span class="token punctuation">:</span> flex<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/* 遮罩层样式 */</span> #mask<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">150</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">150</span>px<span class="token punctuation">;</span> background<span class="token punctuation">:</span> <span class="token function">rgba</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0.3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> position<span class="token punctuation">:</span> absolute<span class="token punctuation">;</span> top<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> left<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span> display<span class="token punctuation">:</span> none<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>style<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span> <span class="token operator"><</span>body<span class="token operator">></span> <span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"box"</span><span class="token operator">></span> <span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"small"</span><span class="token operator">></span> <span class="token operator"><</span>img src<span class="token operator">=</span><span class="token string">"./1.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span> <span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"mask"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>div id<span class="token operator">=</span><span class="token string">"big"</span><span class="token operator">></span> <span class="token operator"><</span>img src<span class="token operator">=</span><span class="token string">"./1.jpg"</span> alt<span class="token operator">=</span><span class="token string">""</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>script<span class="token operator">></span> <span class="token comment">//遮罩层 </span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">mouseenter</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask,#big"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">mouseleave</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask,#big"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">hide</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">mousemove</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//计算遮罩层的变量</span> <span class="token keyword">var</span> x<span class="token operator">=</span>e<span class="token punctuation">.</span>pageX<span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">offset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>left<span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token keyword">var</span> y<span class="token operator">=</span>e<span class="token punctuation">.</span>pageY<span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">offset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>top<span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">//设置最小限制</span> x<span class="token operator"><</span><span class="token number">0</span><span class="token operator">?</span>x<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">:</span>x<span class="token punctuation">;</span> y<span class="token operator"><</span><span class="token number">0</span><span class="token operator">?</span>y<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">:</span>y<span class="token punctuation">;</span> <span class="token comment">//设置最大限制</span> x<span class="token operator">></span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">?</span>x<span class="token operator">=</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> y<span class="token operator">></span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">?</span>y<span class="token operator">=</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#mask"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">css</span><span class="token punctuation">(</span><span class="token punctuation">{</span> left<span class="token punctuation">:</span>x<span class="token operator">+</span><span class="token string">"px"</span><span class="token punctuation">,</span> top<span class="token punctuation">:</span>y<span class="token operator">+</span><span class="token string">"px"</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//计算小盒子和大图片的比例</span> <span class="token keyword">var</span> bilix<span class="token operator">=</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#big img"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">width</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">var</span> biliy<span class="token operator">=</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#big img"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#small"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">height</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//根据遮罩层的top和left 来滚动大盒子的scrollTop和scrollLeft</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#big"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">scrollLeft</span><span class="token punctuation">(</span>x<span class="token operator">*</span>bilix<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#big"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">scrollTop</span><span class="token punctuation">(</span>y<span class="token operator">*</span>biliy<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>html<span class="token operator">></span> </code></pre> <p>本文地址:https://blog.csdn.net/weixin_53587096/article/details/112054003</p> </div> <div class="tags"> 相关标签: <a href="/t/javascript/" target="_blank"> javascript </a> </div> <div class="info-pre-next"> <p> 上一篇: <a href="/article/1454745.html"> HashSet集合保证元素唯一性的源码分析 </a> </p> <p> 下一篇: <a href="/article/1454747.html"> 佳能单反相机70D闪光灯怎么关闭? </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/2084155.html" target="_blank" title="vue+jquery+lodash实现滑动时顶部悬浮固定效果"> <h2> vue+jquery+lodash实现滑动时顶部悬浮固定效果 </h2> </a> </li> <li> <a href="/article/2083788.html" target="_blank" title="JavaScript实现京东购物放大镜和选项卡效果的方法分析"> <h2> JavaScript实现京东购物放大镜和选项卡效果的方法分析 </h2> </a> </li> <li> <a href="/article/2083189.html" target="_blank" title="jquery单行文字向上滚动效果的实现代码"> <h2> jquery单行文字向上滚动效果的实现代码 </h2> </a> </li> <li> <a href="/article/2083146.html" target="_blank" title="JavaScript编程开发中如何使用jquery实现放大镜效果"> <h2> JavaScript编程开发中如何使用jquery实现放大镜效果 </h2> </a> </li> <li> <a href="/article/2081629.html" target="_blank" title="JavaScript编程开发中jquery搜索框效果实现方法"> <h2> JavaScript编程开发中jquery搜索框效果实现方法 </h2> </a> </li> <li> <a href="/article/2081627.html" target="_blank" title="基于jQuery实现的打字机效果"> <h2> 基于jQuery实现的打字机效果 </h2> </a> </li> <li> <a href="/article/2081244.html" target="_blank" title="2种jQuery 实现刮刮卡效果"> <h2> 2种jQuery 实现刮刮卡效果 </h2> </a> </li> <li> <a href="/article/2081231.html" target="_blank" title="jQuery实现带滚动线条导航效果的方法教程"> <h2> jQuery实现带滚动线条导航效果的方法教程 </h2> </a> </li> <li> <a href="/article/2079172.html" target="_blank" title="jQuery轻松实现无缝轮播效果"> <h2> jQuery轻松实现无缝轮播效果 </h2> </a> </li> <li> <a href="/article/2076933.html" target="_blank" title="Jquery的Tabs内容轮换效果实现代码"> <h2> Jquery的Tabs内容轮换效果实现代码 </h2> </a> </li> </ul> </div> </article> </div> </main> <footer><div class="box"><div class="ft_nav"><div class="ft_about"><p>关于网站</p><ul><li><a href="/sitemap.xml" target="_blank" title="网站地图">网站地图</a></li><li><a href="/list/2/" title="最新程序员文章站">最新程序员文章站</a></li></ul></div><div class="ft_contact"><ul><li>本站所有数据收集于网络如有侵犯到您的权益,请联系我们进行下架处理。</li><li class="email_show"></li></ul></div></div><div class="copyright"><div class="cr_left"><p> 备案号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备20058927号</a></p><p>© Copyright © 2020-2022 www.superweb999.com 程序员文章站. </p></div></div></div></footer> </body> </html>