jQuery实现简单的图片查看器(2)

pictureViewer = { picInfos: [], curPicIndex: 0, isBig:false,//大图查看?默认为false。默认图片显示区域为window宽度的40%,大图为80% imgTapSelector:".imgTapDetail", init: function(picInfos,tapNumber,isBig){ var _this = this; _this.picInfos = picInfos; _this.curPicIndex = tapNumber; _this.isBig = isBig; tapImgInit(); //图片查看器初始化 function tapImgInit(){ //页面代码和事件只需要初始化一次即可 if(!_this.guid){ _this.guid = 1; initTapImgHtml(); $(document).on("click","#rightTap",function(){ _this.curPicIndex++; if(_this.curPicIndex == _this.picInfos.length){ _this.curPicIndex = 0; } $("#tapContent").html("") tapImg() }).on("click","#leftTap",function(){ _this.curPicIndex--; if(_this.curPicIndex < 0){ _this.curPicIndex = _this.picInfos.length-1; } $("#tapContent").html("") tapImg(); }).on("click","#closeTap",function(){ $(_this.imgTapSelector).hide("fast") }) } var offTop = top.document.body.scrollTop || top.document.documentElement.scrollTop; $('#imgTapPanel').css("margin-top",_this.isBig?(offTop>30?offTop:30):offTop + 100) tapImg(); $(_this.imgTapSelector).show("fast"); } //初始化图片查看器的html代码 function initTapImgHtml(){ var $detailText = '<div >' + '<divwidget-big":"")+'">' + '<div></div>' + '<div></div>' + '<div></div>' + '<div><div>' + '<div>' + '<div>' + '<img src="https://www.jb51.net/logo.png"><span></span>' + '</div>' + '<div></div>' + '</div></div></div></div></div>'; $(_this.imgTapSelector).html($detailText); hoverButton("#leftTap", 0, 0, 0, "-80px"); hoverButton("#rightTap", "-80px", 0, "-80px", "-80px"); hoverButton("#closeTap", 0, "-165px", "-60px", "-165px"); } //添加左右切换图标、关闭图标的hover响应事件。这个其实可以起到css中更好一些 function hoverButton(id, x, y, A, B){ $(id).hover(function(){ $(this).css('background-position', A + ' '+ B) },function(){ $(this).css('background-position', x + ' '+ y) }) } //刷新当前图片及图片信息 function tapImg(){ var reg = /[::]$/, leftTap = $("#leftTap"), rightTap = $("#rightTap"), imgTap = $("#tapImg"), contentTap = $("#tapContent"); leftTap.css("display","block"); rightTap.css("display","block"); if(_this.picInfos.length == 1){ leftTap.css("display","none"); rightTap.css("display","none"); } imgTap.attr("src",_this.picInfos[_this.curPicIndex].url); var data = _this.picInfos[_this.curPicIndex].data, dataLength = data.length, $text = ""; for(var i=0; i<dataLength; i++){ $text += '<div>' + '<label>'+data[i].key.replace(reg,"")+':</label>' + '<span>'+data[i].value+'</span>' + '</div>'; }; contentTap.empty().append($text); setTimeout(function(){ if(_this.isBig && imgTap.height() > 400){ imgTap.parent().attr("style","height:inherit"); }else{ imgTap.parent().removeAttr("style"); } },1); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwxzz.html