jquery插件實(shí)現(xiàn)圖片對(duì)比
本文實(shí)例為大家分享了jquery插件實(shí)現(xiàn)圖片對(duì)比的具體代碼,供大家參考,具體內(nèi)容如下
很常見的一個(gè)效果,做起來(lái)不難
效果如下
代碼部分
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>做圖片對(duì)比</title> <script src="js/jquery-3.4.1.min.js"></script> <style> *{ margin: 0px; padding: 0px; user-select: none; } .div{ border: 1px solid lightgray; width: 400px; height: 200px; margin: 10px; float: left; position: relative; } .img1{ position: absolute; top: 0; bottom: 0; left: 0; width: 50%; } .img2{ position: absolute; top: 0; bottom: 0; left: 50%; right: 0; } .img1,.img2{ background-position: center center; background-size: 400px 200px; background-repeat: no-repeat; } .img1{ background-position-x: 0; } .img2{ background-position-x: 100%; filter: invert(100%); } .bar{ position: absolute; top: 0; bottom: 0; right:-4px; width: 8px; background-color: gray; cursor: ew-resize; opacity: 0.2; } .stop{ pointer-events: none; } </style> </head> <body> <div class="div"> <div class="img1" style="background-image: url(img/1.jpg);"> <div class="bar" data-flag="0"></div> </div> <div class="img2" style="background-image: url(img/1.jpg);"></div> </div> <div class="div"> <div class="img1" style="background-image: url(img/2.jpg);"> <div class="bar" data-flag="0"></div> </div> <div class="img2" style="background-image: url(img/2.jpg);"></div> </div> </body> </html> <script> $(document).ready(function(){ $(".bar").mousedown(function(){ $(this).parent().addClass("stop"); $(this).parent().next().addClass("stop"); $(this).attr("data-flag","1") }) $(".div").mousemove(function(e){ var temp = $(this).find('.bar').attr("data-flag"); if(temp=="1"){ var w = $(this).width(); var x = e.offsetX; var p = parseFloat((x/w).toFixed(2))*100; $(this).children(".img1").css('width',p+'%'); $(this).children(".img2").css('left',p+'%'); } }) $(document).mouseup(function(){ $(".img1,.img2").removeClass("stop"); $(".bar").attr("data-flag","0") }) }) </script>
思路解釋
感覺很簡(jiǎn)單,就是倆圖作為背景圖片然后控制他的布局位置,控制容器的寬高就行了
需要做適應(yīng)性優(yōu)化的還有背景圖大小的控制,當(dāng)然父容器不會(huì)變化就不會(huì)出問(wèn)題
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery中的常用事件bind、hover、toggle等示例介紹
這篇文章主要介紹了jquery中常用的事件,比如$(document).ready()、bind、hover、toggle、click等等,并有示例,易于學(xué)習(xí)2014-07-07Jquery進(jìn)度條插件 Progress Bar小問(wèn)題解決
今天遇到一個(gè)進(jìn)度條的小問(wèn)題,搞的我小糾結(jié)了一會(huì),最后感謝同事分享文章,得以結(jié)局,呵呵,小經(jīng)驗(yàn)還是要保存的2011-07-07jQuery將所有被選中的checkbox某個(gè)屬性值連接成字符串的方法
這篇文章主要介紹了jQuery將所有被選中的checkbox某個(gè)屬性值連接成字符串的方法,可實(shí)現(xiàn)將選中屬性值連接成字符串的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01jQuery實(shí)現(xiàn)網(wǎng)頁(yè)抖動(dòng)的菜單抖動(dòng)效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)網(wǎng)頁(yè)抖動(dòng)的菜單抖動(dòng)效果,鼠標(biāo)滑過(guò)菜單項(xiàng)可見到菜單項(xiàng)的抖動(dòng)效果,涉及jquery鼠標(biāo)事件及頁(yè)面元素樣式動(dòng)態(tài)操作的技巧,需要的朋友可以參考下2015-08-08jquery ajax雙擊div可直接修改div中的內(nèi)容
這篇文章主要介紹了jquery ajax雙擊div直接修改div中內(nèi)容的相關(guān)方法,感興趣的小伙伴們可以參考一下2016-03-03jQuery實(shí)現(xiàn)行文字鏈接提示效果的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)行文字鏈接提示效果的方法,實(shí)例分析了jQuery操作文字提示效果的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03jquery中animate的stop()方法作用實(shí)例分析
這篇文章主要介紹了jquery中animate的stop()方法作用,實(shí)例分析了animate的stop()方法在視頻播放代碼中的使用技巧,感興趣的朋友可以對(duì)比分析一下帶stop方法與不帶stop方法運(yùn)行效果的區(qū)別,需要的朋友可以參考下2015-01-01jQuery Form 表單提交插件之formSerialize,fieldSerialize,fieldValue,r
這篇文章主要介紹了jQuery Form 表單提交插件之formSerialize,fieldSerialize,fieldValue,resetForm,clearForm,clearFields的應(yīng)用的相關(guān)資料,需要的朋友可以參考下2016-01-01