jquery 圖片點擊放大預(yù)覽功能詳解
一個簡單相冊預(yù)覽功能,可縮放可拖動,只是覺得這個功能應(yīng)該很常用,先記錄下來方便下次
代碼也都是我在項目中摳出來的,如果有什么問題歡迎留言討論

html
圖片列表的樣式我就不寫出來了 但是下面的class 和 布局結(jié)構(gòu) 都很重要:
<div>
<div><img src="images/banner.jpg" ></div>
<div><img src="images/banner.jpg" ></div>
<div><img src="images/banner.jpg" ></div>
<div><img src="images/banner.jpg" ></div>
</div>查看圖片的彈窗代碼(核心):
<div></div> <div> <div>×</div> <div> <ul > </ul> </div> <div> <div>?</div> <div>?</div> </div> </div>
彈窗的樣式
.seeImg_mask{ width:100%; height: 100%; position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px; background: rgba(0,0,0,0.7); margin: auto; z-index: 999; display: none; }
.seeImg_popup{ width: 80%; height: 80%; background: #fff; position: fixed; bottom: 200%; left: 10%; right: 0px; margin: auto; border: 10px rgba(0,0,0,0.2) solid; border-radius: 10px; z-index: 9999; }
.seeImg_close{ width: 40px; height: 40px; line-height: 35px; text-align: center; font-size: 28px; color: #fff; position: absolute; right: 0px; top: 0px; background: rgba(226, 36, 36, 0.72); cursor: pointer; }
.seeImg_swrieb{ width: 90%; height: 100%; margin: 0px auto; position: relative; overflow: hidden; }
.seeImg_list{ height: 100%; width: auto; display: flex; flex-direction: row; transition: all 0.5s; }
.seeImg_list li{ height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; position: relative; overflow: hidden; }
.seeImg_list li img{
height: 90%;display: block;position: absolute; top: -160px;cursor: all-scroll;
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Chrome/Safari/Opera */
-khtml-user-select: none; /* Konqueror */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently not supported by any browser */
padding: 200px 0px;
z-index: 555;
}
.seeImg_fun{ width: 100%; height: 40px; line-height: 40px; display: flex; flex-direction: row; justify-content: space-between; box-sizing: border-box; padding: 0px 15px; position: absolute; top: 0px; bottom: 0px; left: 0px; right: 0px; margin: auto; }
.fun_label{ font-size: 50px; color: #777; cursor: pointer; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }js部分
$(document).on("click",".seeimg",function(){
$(".seeImg_list li").remove();
var seeImglistLiLeng = $(this).parents(".imgnyr").children(".img-list").length;
$(".seeImg_popup").attr("data",seeImglistLiLeng)
var arrtimg = [];
$(this).parents(".imgnyr").children(".img-list").each(function(i){
var imgse = $(this).children("img").attr("src");
arrtimg.push(imgse)
});
for( var i in arrtimg){
var html ="";
html +="<li><img src=" +arrtimg[i]+ " class='enlarge' draggable='false' onmousewheel='return bbimg(this)' /></li>";
$(".seeImg_list").append(html)
}
var seeImgswriebWight = $(".seeImg_swrieb").width();
var seeImglistWigth = seeImgswriebWight * seeImglistLiLeng;
console.log(seeImglistWigth)
$(".seeImg_mask").show();
$(".seeImg_popup").css("bottom","10%");
$(".seeImg_list").width(seeImglistWigth)
$(".seeImg_list").css("margin-left","0px")
$(".seeImg_list li").width(seeImgswriebWight)
$(".seeImg_list").children("li").each(function(i){
$(this).children("img").attr("data",i)
$(this).children("img").addClass("maxdom"+i)
})
})
$(document).on("click",".seeImg_close",function(){
$(".seeImg_mask").hide();
$(".seeImg_popup").css("bottom","200%");
})
var UlMarRig = 0;
$(document).on("click",".labelRight",function(){
var roue = $(".seeImg_swrieb").width();
var mes = $(".seeImg_popup").attr("data");
var csdx = roue * mes;
console.log(csdx)
UlMarRig = UlMarRig - roue;
if( UlMarRig <= -csdx){
UlMarRig = 0;
}
$(".seeImg_list").css("margin-left", UlMarRig)
})
$(document).on("click",".labelLeft",function(){
var emgd = $(".seeImg_swrieb").width();
var aas = $(".seeImg_popup").attr("data");
var ddx = emgd * aas;
if( UlMarRig == 0){
UlMarRig = -ddx;
}
UlMarRig = UlMarRig + emgd;
$(".seeImg_list").css("margin-left", UlMarRig)
})
var ment = 90;
function bbimg(o){
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
console.log(zoom)
if(zoom>=110){
ment = ment +10;
o.style.height = ment +'%';
}
if(zoom<110){
ment = ment -10;
o.style.height = ment +'%';
}
// console.log(ment)
}
$(document).on("mousedown",".enlarge",function(e){
var u = $(this).attr("data");
// e.pageX
var imtLeft = $(this).position().left;
var imtTop = $(this).position().top;
var distenceX = e.pageX - imtLeft; //記錄鼠標(biāo)點擊的位置與div左上角水平方向的距離
var distenceY = e.pageY - imtTop; //記錄鼠標(biāo)點擊的位置與div左上角數(shù)值方向的距離
$(document).mousemove(function(e){
var x = e.pageX - distenceX;
var y = e.pageY - distenceY;
if(x<0){
x= x ;
}else if(x>$(document).width()-$('.maxdom'+u).outerWidth(true)){
x = $(document).width()-$('.maxdom'+u).outerWidth(true);
}
if(y<0){
y= y;
}else if(y>$(document).height()-$('.maxdom'+u).outerHeight(true)){
y = $(document).height()-$('.maxdom'+u).outerHeight(true);
}
$('.maxdom'+u).css({'left':x+'px','top':y+'px'});
});
$(document).mouseup(function(){
$(document).off('mousemove'); //移除鼠標(biāo)移動事件
});
})感興趣的朋友可以使用如下在線運行工具測試上述代碼運行效果:
http://tools.jb51.net/code/HtmlJsRun
http://tools.jb51.net/code/WebCodeRun
- jQuery實現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁面布局
- 基于jquery的防止大圖片撐破頁面的實現(xiàn)代碼(立即縮放)
- jQuery+css實現(xiàn)的點擊圖片放大縮小預(yù)覽功能示例【圖片預(yù)覽 查看大圖】
- jQuery實現(xiàn)鼠標(biāo)滑過商品小圖片上顯示對應(yīng)大圖片功能【測試可用】
- jQuery實現(xiàn)鼠標(biāo)滑過預(yù)覽圖片大圖效果的方法
- jQuery實現(xiàn)大圖輪播
- jQuery實現(xiàn)的小圖列表,大圖展示效果幻燈片示例
- jquery插件jquery.LightBox.js實現(xiàn)點擊放大圖片并左右點擊切換效果(附demo源碼下載)
- jQuery實現(xiàn)點擊小圖片淡入淡出顯示大圖片特效
- jQuery實現(xiàn)點擊查看大圖并以彈框的形式居中
- 基于jQuery插件實現(xiàn)點擊小圖顯示大圖效果
- jquery實現(xiàn)移動端點擊圖片查看大圖特效
- jQuery實現(xiàn)點擊小圖顯示大圖代碼分享
相關(guān)文章
jQuery怎么解析Json字符串(Json格式/Json對象)
可以利用jquery或js解析json數(shù)據(jù),下面與大家分享下jquery解析json字符串方法,一個是解析Json格式,一個是解析Json對象,感興趣的朋友可以學(xué)習(xí)下2013-08-08
jquery實現(xiàn)仿JqueryUi可拖動的DIV實例
這篇文章主要介紹了jquery實現(xiàn)仿JqueryUi可拖動的DIV,實例分析了jquery模擬jqueryUI實現(xiàn)div拖動的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
JS拖動選擇table里的單元格完整實例【基于jQuery】
這篇文章主要介紹了JS拖動選擇table里的單元格,結(jié)合完整實例形式分析了基于jQuery的table表格動態(tài)操作相關(guān)實現(xiàn)技巧,涉及事件響應(yīng)及頁面元素屬性動態(tài)操作使用方法,需要的朋友可以參考下2019-05-05
jQuery 1.4 15個你應(yīng)該知道的新特性(譯)
jQuery 1.4 最近剛剛發(fā)布. 這個版本可不是一個簡單的改進(jìn),它不僅包含了很多新的特性,還改進(jìn)了很多功能, 更在性能優(yōu)化方面下了很大功夫, 本文將對這些新的特性和增強(qiáng)的部分進(jìn)行討論,希望能對你有所幫助.2010-01-01
jquery UI Datepicker時間控件的使用方法(基礎(chǔ)版)
這篇文章主要介紹了jquery ui datepicker時間控件的使用方法,需要的朋友可以參考下2015-11-11
jQuery實現(xiàn)返回頂部按鈕和scroll滾動功能[帶動畫效果]
這篇文章主要介紹了jQuery實現(xiàn)返回頂部按鈕和scroll滾動功能[帶動畫效果],需要的朋友可以參考下2017-07-07

