jquery插件實(shí)現(xiàn)圖片對(duì)比
本文實(shí)例為大家分享了jquery插件實(shí)現(xiàn)圖片對(duì)比的具體代碼,供大家參考,具體內(nèi)容如下
很常見(jiàn)的一個(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>
思路解釋
感覺(jué)很簡(jiǎn)單,就是倆圖作為背景圖片然后控制他的布局位置,控制容器的寬高就行了
需要做適應(yīng)性優(yōu)化的還有背景圖大小的控制,當(dāng)然父容器不會(huì)變化就不會(huì)出問(wèn)題
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery中ztree 點(diǎn)擊文本框彈出下拉框的實(shí)例代碼
這篇文章主要介紹了jQuery中ztree 點(diǎn)擊文本框彈出下拉框的實(shí)例代碼 的相關(guān)資料,需要的朋友可以參考下2017-02-02
為你的網(wǎng)站增加亮點(diǎn)的9款jQuery插件推薦
如今,jQuery插件眾多,滿足各種各樣的應(yīng)用需求。 在這篇文章中,我收集了9款很棒的插件,最喜歡的是Sausage內(nèi)容分頁(yè)插件,作者想法特別新穎!希望你能從中找到自己需要的插件。2011-05-05
jquery 通過(guò)ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法
今天小編就為大家分享一篇jquery 通過(guò)ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
用jquery統(tǒng)計(jì)子菜單的條數(shù)示例代碼
統(tǒng)計(jì)子菜單條數(shù)的方法有很多,在本文為大家詳細(xì)介紹下使用jquery是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過(guò)2013-10-10
解決Jquery鼠標(biāo)經(jīng)過(guò)不?;瑒?dòng)的問(wèn)題
在鼠標(biāo)經(jīng)過(guò)的時(shí)候不停的顯示隱藏html元素,正確的寫法應(yīng)該是下面這樣的,需要的朋友可以參考下2014-03-03
jQuery實(shí)現(xiàn)帶幻燈的tab滑動(dòng)切換風(fēng)格菜單代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)帶幻燈的tab滑動(dòng)切換風(fēng)格菜單代碼,可實(shí)現(xiàn)點(diǎn)擊菜單項(xiàng)進(jìn)行對(duì)應(yīng)內(nèi)容的滑動(dòng)切換功能,涉及jquery鼠標(biāo)事件及頁(yè)面元素屬性的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2015-08-08
jQuery獲取單擊節(jié)點(diǎn)對(duì)象的方法
這篇文章主要介紹了jQuery獲取單擊節(jié)點(diǎn)對(duì)象的方法,涉及jQuery操作鼠標(biāo)事件與頁(yè)面元素的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

