search
HomeWeb Front-endHTML Tutorial基于jQuery的一个简单的图片查看器_html/css_WEB-ITnose

  项目中自己diy了一个图片查看器。因为初始代码不是自己的,只是在上面改了一下也没有弄的很漂亮。等以后有时间了在重写一下样式和封装,作为备用的只是积累吧。如果有童鞋有用到,完全可以在此基础上改,比较容易,代码也比较简单

  图片查看器主要有几个功能:

  1.显示图片和图片信息(图片名称、发布者等等)

  2.切换图片

  3.关闭图片查看器

 

初始化接口函数pictureViewer.init: function(picInfos,tapNumber,isBig)

  picInfos: 传入图片组信息,必须,格式如下

    var picInfos = [        {          "url" : "default.png",          "data": [            {              "key":"名称:",              "value":"测试图片"            },            {              "key":"发布者:",              "value":"chua"            }          ]        },        {          "url" : "test.jpeg",          "data": [            {              "key":"名称",              "value":"测试图片"            },            {              "key":"发布者:",              "value":"发大水发大水发顺风h"            },            {              "key":"这个图片的其他信息",              "value":"vsfsgsdgfds234323424"            }          ]        },        ...    ] //传入参数的样式 

  tapNumber: 要显示的图片在图片列表中的索引,必须,从0开始

  isBig:是否使用大图查看,默认是false,可选

  

  html及css源码如下(后面有一个例子)

<!DOCTYPE html><html lang="ch-cn">  <head>  <meta charset="utf-8">  <script type="text/javascript" src='jquery-1.9.1.js'></script>    <link rel="stylesheet" type="text/css" href="imgTap.css">    <script type="text/javascript" src='imgTap.js'></script>    <style type="text/css">    html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{      margin: 0;      padding:0;    }    *{      -webkit-box-sizing: border-box;      -moz-box-sizing: border-box;      box-sizing: border-box;    }    body{      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;    }    .imgTapDetail {        display: none;        position: absolute;        z-index: 2000;        top: 0;        width: 100%;        height: 100%;        background: none transparent scroll repeat 0% 0%;        color: #000 !important;    }    .page-secShadow {        position: absolute;        top: 0;        left: 0;        width: 100%;        height: 100%;        background: #000;        background-color: rgba(0,0,0,.3);        filter: alpha(opacity=50);    }    .page-shadowContent {        width: 40%;        margin: 0 auto;        margin-top: 20%;        position: relative !important;        min-width: 400px;    }    .page-shadowContent.widget-big{    }    .leftTap, .rightTap {        margin-top: 50%;        width: 80px;        height: 80px;        border-radius: 50%;        position: absolute;        top: -80px;        cursor: pointer;    }    .leftTap {        left: -30%;        background: url("imgTap.png") 0 0 no-repeat;    }    .rightTap {        right: -30%;        background: url("imgTap.png") -80px 0 no-repeat;    }    .closeTap {        width: 60px;        height: 60px;        border-radius: 50%;        position: absolute;        right: -30px;        top: -30px;        cursor: pointer;        background: url("imgTap.png") 0 -165px no-repeat;        z-index: 99;    }    .widget {        padding: 0 5px;        -webkit-box-shadow: none;        -moz-box-shadow: none;        box-shadow: none;        margin: 3px 0 10px 0;    }    .widget-body {        background-color: #fff;        -webkit-box-shadow: 1px 0 10px 1px rgba(0,0,0,.3);        -moz-box-shadow: 1px 0 10px 1px rgba(0,0,0,.3);        box-shadow: 1px 0 10px 1px rgba(0,0,0,.3);        padding: 12px;    }    .page-shadowContent .widget-body {        min-height: 200px;        padding: 15px!important;        border: 1px solid #ccc;        overflow: hidden;    }    .widget-body .row {        margin-right: -10px !important;        margin-left: -10px !important;    }    .imgShow {        text-align: center;        height: 400px;        padding-left: 10px;        padding-right: 10px;        /*background: url(default.png) center no-repeat;*/    }    #tapContent{        padding-left: 10px;        padding-right: 10px;    }    .imgContent {        max-width: 400px;        max-height: 400px;        vertical-align: middle;    }    .widget-body img {        max-width: 100%;        height: auto!important;    }    .imgShow > span {        height: 100%;        display: inline-block;        vertical-align: middle;    }        .imgTapDetail .form-group {        overflow: hidden;        margin-bottom: 0 !important;        position: relative;        min-height: 34px;    }    .widget-detail .form-group .detail-LabelStyle {        float: left;        padding-left: 5px;        /* max-width: 50%; */        text-align: left;        line-height: 34px!important;        color: rgb(115, 115, 115);        padding-right: 5px;        height: 34px;        overflow: hidden;        left: 0;        top: 0;    }    .widget-detail .form-group .detail-SpanStyle {        padding: 8px 0 6px 5px;        line-height: 20px;        width: auto;        height: auto!important;        min-height: 34px;        float: left;        /* margin-left: 80px; */        word-break: break-all;    }    .widget-big {        width: 80%;        min-width: 300px;    }    .widget-big .leftTap{        left: -10%;    }    .widget-big .rightTap{        right: -10%;    }    .widget-big .widget-detail{        padding: 40px 0;    }    .widget-big .imgShow{        min-height: 500px;    }    .widget-big .imgShow img{        max-width: 800px;         max-height: 550px;    }    </style>  </head>  <body >    <div class="imgTapDetail"></div>    <script type="text/javascript">    var picInfos = [      {        "url" : "default.png",        "data": [          {            "key":"名称:",            "value":"测试图片"          },          {            "key":"发布者:",            "value":"chua"          }        ]      },      {        "url" : "test.jpeg",        "data": [          {            "key":"名称",            "value":"测试图片"          },          {            "key":"发布者:",            "value":"发大水发大水发顺风h"          },          {            "key":"这个图片的其他信息",            "value":"vsfsgsdgfds234323424"          }        ]      }    ]     pictureViewer.init(picInfos,0,true);    </script>  </html>

  js的源码如下

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 class="page-secShadow" >'                + '<div id="imgTapPanel" class="page-shadowContent '+(_this.isBig?"widget-big":"")+'">'                + '<div id="leftTap" class="leftTap"></div>'                + '<div id="rightTap" class="rightTap"></div>'                + '<div id="closeTap" class="closeTap"></div>'                + '<div class="widget row"><div class="widget-body">'                + '<div class="widget-detail row">'                + '<div class="imgShow">'                + '<img  class="imgContent lazy"  src="/static/imghwm/default1.png"  data-src="logo.png"  id="tapImg" alt="基于jQuery的一个简单的图片查看器_html/css_WEB-ITnose" ><span></span>'                + '</div>'                + '<div id="tapContent"></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 class="form-group">'                    + '<label class="detail-LabelStyle">'+data[i].key.replace(reg,"")+':</label>'                    + '<span id="spanLoginName" class="detail-SpanStyle">'+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);        }    }}

  css中用到的切图imgTap.png为下面的图片

  

  测试例子pictureViewer.init(picInfos,0,true);的效果图如下

  测试例子pictureViewer.init(picInfos,0,false);的效果图如下

  这是一个比较粗糙的图片查看器,改起来也比较方便。后期如果有时间本人重写一下。

   

  如果觉得本文不错,请点击右下方【推荐】!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.