运用jQuery实现的图片浏览器
来源:互联网 发布:手机管理照片 知乎 编辑:程序博客网 时间:2024/05/22 06:30
function imageGally(opt){
var targetDivId=opt.targetDivId; //-----主框架ID
var currentImageIndex = opt.currentImageIndex; //----显示当前图片索引的容器ID
var totalImage = opt.totalImage; //显示图片总数的容器ID
var bigImagePre = opt.bigImagePre; //大图片向前ID
var bigImageNxt = opt.bigImageNxt; //大图片向后ID
var bigImageCnt = opt.bigImageCnt; //显示当前大图片的容器ID
var smlImagePre = opt.smlImagePre; //小图片向前ID
var smlImageNxt = opt.smlImageNxt; //小图片向后ID
var smlImageCnt = opt.smlImageCnt; //小图片导航栏ID
var imgDataSrc = opt.dataSrc; //数据源[{ImageName:'a1',ImageUrl:'2.jpg'},{ImageName:'a2',ImageUrl:'1.jpg'}]
var imgMaxWidth = opt.MaxWidth; //大图片的最宽
var imgMaxHight = opt.MaxHight; //大图片的最高
var __imageCount= imgDataSrc .length;
var __currentImageIndex = 0;
if (__imageCount == 0) {
$(targetDivId).css({"text-align":"center"}).html("<img src='http://www.mainaer.com/uploadfiles/mainaer/nopic.gif");
return;
}
var imgaePositon=new Object ();
for(var ci=0;ci<__imageCount ;ci ++){
imgaePositon[imgDataSrc[ci].ImageUrl ] = ci ;
}
//-------------显示图片总数
$(totalImage).html(__imageCount );
//---------------创建大图片并显示出来
var createBigImage=function(imgUrl){
var FitWidth =imgMaxWidth ;
var FitHeight=imgMaxHight ;
var ImgD={ width: FitWidth , height:FitHeight };
var image=new Image();
image.src= imgUrl ;
if(image.width>0 && image.height>0){
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
} else{
if(image.height>FitHeight){
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
$(bigImageCnt ).empty();
$("<img />").attr("src",imgUrl).css({ "width": ImgD.width ,"height":ImgD.height }).appendTo(bigImageCnt );
}
var showCurrentImageIndex = function(url) {
__currentImageIndex = imgaePositon[url];
$(currentImageIndex).html(__currentImageIndex + 1);
$(smlImageCnt + ">img").attr("style", "");
$(smlImageCnt + ">img[src='" + url + "']").css({ "border": "1", "border-style": "solid", "border-color": "yellow" });
}
var preappendSmallImage=function(imgUrl){
$(smlImageCnt +">img:last").remove();
$("<img />").attr("src",imgUrl).click(function(){
createBigImage($(this).attr("src"));
showCurrentImageIndex($(this).attr("src"));
}).prependTo(smlImageCnt);
}
var appendSmallImage=function(imgUrl){
$(smlImageCnt +">img:first").remove();
$("<img />").attr("src",imgUrl).click(function(){
createBigImage($(this).attr("src"));
showCurrentImageIndex($(this).attr("src"));
}).appendTo(smlImageCnt);
}
var getCuurentBigImagePostion=function (){
var imgUrl=$(bigImageCnt +">img:first")[0].src ;
return imgaePositon[ imgUrl ] ;
}
$(smlImageNxt ).click(function (){
var lastImgUrl= $(smlImageCnt +">img:last")[0].src;
var lastPostion=imgaePositon[lastImgUrl] ;
if(lastPostion < __imageCount-1 ){
appendSmallImage( imgDataSrc[lastPostion+1].ImageUrl) ;
} else {
alert("没有啦!");
}
});
$(smlImagePre ).click(function (){
var firstImgUrl= $(smlImageCnt +">img:first")[0].src;
var firstPostion= imgaePositon[firstImgUrl] ;
if(firstPostion > 0 ){
preappendSmallImage( imgDataSrc[firstPostion-1].ImageUrl) ;
} else {
alert("没有啦!");
}
});
$(bigImageNxt).click(function() {
var imgPosition = getCuurentBigImagePostion();
if (imgPosition < __imageCount - 1) {
var nextUrl = imgDataSrc[imgPosition + 1].ImageUrl;
createBigImage(nextUrl);
appendSmallImage(nextUrl);
showCurrentImageIndex(nextUrl);
} else {
alert("没有啦");
}
});
$(bigImagePre).click(function() {
var imgPosition = getCuurentBigImagePostion();
if (imgPosition >= 1) {
var preUrl = imgDataSrc[imgPosition - 1].ImageUrl;
createBigImage(preUrl);
preappendSmallImage(preUrl);
showCurrentImageIndex(preUrl);
} else {
alert("没有啦");
}
});
//----------------添加小图片导航页面
//----------同时初始化大图片和小图片
for(var i=0;i<__imageCount && i< 6 ;i++){
var curl=imgDataSrc[i].ImageUrl ;
$("<img />").attr("src",curl).click(function(){
createBigImage($(this).attr("src"));
showCurrentImageIndex($(this).attr("src"));
}).appendTo(smlImageCnt);
if( i==0){
createBigImage(curl );
showCurrentImageIndex(curl );
};
}
}
------------------------------------HTML模板------------------
<div class="Wbox" id="proImageContentDiv0">
<div class="WZ">
<p>页码:<span id="proImagePageIndex0">0</span>of<span id="proImageCount0">0</span></p>
</div>
<div class="C">
<div class="one"><input type="image" class="Button6" src="http://www.mainaer.com/skin/mainaer/images/button_l.gif" id="bigPicPre0"></div>
<div style="float:left;">
<div class="two"><a target="_blank" style="vertical-align:middle" id="bigImageCnt0"><img src="http://www.mainaer.com/uploadfiles/mainaer/loading1.gif" width="100" height="100"></a></div>
</div>
<div class="three"><input type="image" class="Button7" src="http://www.mainaer.com/skin/mainaer/images/button_r.gif" id="bigPicNxt0"></div>
</div>
<div style="clear:both;"></div>
<div class="Bbox">
<div class="BBC">
<div class="BL"><input type="image" class="Button8" src="http://www.mainaer.com/skin/mainaer/images/button_l2.gif" id="smlPicPre0"></div>
<div class="BC" id="smlImageContent0"></div>
<div class="BR"><input type="image" class="Button9" src="http://www.mainaer.com/skin/mainaer/images/button_r2.gif" id="smlPicNxt0"></div>
</div>
</div>
</div>
----------------------------调用方法-----------------------
var h1=[]; imageGally({targetDivId:"#proImageContentDiv0",
currentImageIndex:"#proImagePageIndex0",
totalImage:"#proImageCount0",
bigImagePre:"#bigPicPre0",bigImageNxt:"#bigPicNxt0",bigImageCnt:"#bigImageCnt0",smlImagePre:"#smlPicPre0",smlImageNxt:"#smlPicNxt0",smlImageCnt:"#smlImageContent0",
dataSrc :h0,MaxWidth:530,MaxHight:350});
--------------------效果展示页面:-----------------------
http://hangzhou.mainaer.com/shangchengqu/4.htm
- 运用jQuery实现的图片浏览器
- jquery实现兼容浏览器的图片上传本地预览功能
- jquery实现兼容浏览器的图片上传本地预览功能
- 图片浏览器的接口设计-桥接模式的运用
- 简单图片浏览器的实现
- js实现的图片浏览器
- 上传图片预览功能的IE浏览器兼容性问题的实现:JS+JQuery+CSS完整版
- WebView组件浏览器实现运用.
- JQuery实现的图片翻转
- jquery判断当前浏览器的实现代码
- jquery的ajax运用
- Jquery的运用
- jquery.vilidate的运用
- SwingWorker实现的简易图片浏览器
- java swing实现简单的图片浏览器
- Gallery实现幻灯片式的图片浏览器
- Qt 实现的简易本地图片浏览器
- canvas实现简单的图片浏览器
- [转]《JMeter从入门到精通》之一——开始你的第一个JMeter脚本
- SubVersion服务器Windows安装指南
- 堆的管理
- ASP常用类之——纯真IP库使用类
- 28句话,句句触动你的心
- 运用jQuery实现的图片浏览器
- PHP模拟实现Java的endsWith
- 控件随着窗口大小变化
- 成为Oracle DBA最基本的十步
- Request.UrlReferrer注意
- mysql错误: Cannot convert value '0000-00-00 00:00:00' from column 10 to TIMESTAMP
- 关于SVN 使用中的一些小技巧
- Oracle Database中DBA常用的表和视图
- 在VS2008中创建自定义控件(七)自定义控件的中常用到的特性