CSS+JS實現圖片集展示

來源:互聯網
上載者:User

標籤:圖片   css   

首先,說說實現的效果:

1、圖片的展示與翻頁;

2、點擊圖片放大圖片。


實現的效果如下所示:


初始化和第一張


中間的圖片


最後一張圖片


單擊放大顯示圖片詳細與資訊


實現的內容很簡單,是常規圖片的展示方式,下面說說我的實現思路。

1、圖片的展示與翻頁

a、圖片展示

圖片展示是通過<img />標籤實現的:

<div class="container">    <div class="image-list">        <div id="prev" onclick="updateImage('prev')"></div>        <a id="img1" class="thumb-a" onclick="showImg(1)">            <img class="thumb-img" width="200" height="150" src="img/demopage/thumb-1.jpg" alt="" />        </a>        <a  id="img2" class="thumb-a thumb-a-hide" onclick="showImg(2)">            <img class="thumb-img" width="200" height="150" src="img/demopage/thumb-2.jpg" alt="" />        </a>        <a  id="img3" class="thumb-a thumb-a-hide" onclick="showImg(3)">            <img class="thumb-img" width="200" height="150" src="img/demopage/thumb-3.jpg" alt="" />        </a>        <div id="next"  onclick="updateImage('next')"></div>    </div></div>
b、翻頁

翻頁是通過updateImage這個函數實現的,傳遞參數為type,判斷操作時“上一張”還是“下一張”,updateImage函數如下:

        function updateImage(type){            if(type==="prev"){                if(imgIndex>1){                    imgIndex=imgIndex-1;                }            }            else{                if(imgIndex<3){                    imgIndex=imgIndex+1;                }            }            for(var i=0;i<3;i++){                $("#img"+(i+1)).addClass("thumb-a-hide");            }            $("#img"+imgIndex).removeClass("thumb-a-hide");            if(imgIndex===3){                $("#next").hide();            }            else{                $("#next").show();            }            if(imgIndex===1){                $("#prev").hide();            }            else{                $("#prev").show();            }        };
函數中,imgIndex記錄的是當前顯示的圖片的編號。

①、判斷操作類型,並設定作業後的圖片的編號。

②、迴圈添加thumb-a-hide樣式,隱藏所有的圖片,並移除圖片編號為imgIndex的thumb-a-hide樣式,顯示圖片;

③、根據imgIndex判斷操作按鈕的顯示與隱藏。


2、點擊顯示圖片詳情與資訊

該效果通過函數showImg實現,showImg的具體內容如下:

        function showImg(index){            var width=600,height=400;            var winWidth = $(window).width(),winHeight = $(window).height();            var modalBg = $("<div></div>");            modalBg.addClass("modal-bg");            modalBg.appendTo($('body'));            var modal = $("<div></div>");            modal.addClass("modal");            modal.css("position","absolute")                 .css("top",(winHeight-height)/2+"px")                 .css("left",(winWidth-width)/2+"px");            var titleTipsBg = $("<div></div>").addClass("tips-bg");            var titleTips = $("<a></a>").addClass("tips").html("I am a Chinese.");            var titleClose =  $("<a></a>").addClass("close");            var title = $("<div></div>");            title.addClass("title");            title.append(titleTipsBg)                 .append(titleTips)                 .append(titleClose);            titleClose.on("click",function(){                modalBg.hide();                modal.hide();            });            title.appendTo(modal);            var img = $("<img>").attr("width",width)                                 .attr("height",height)                                 .attr("src","img/demopage/image-"+index+".jpg");            var imgDiv = $("<div></div>").append(img);            imgDiv.appendTo(modal);            modal.appendTo($('body'));        }

上述代碼產生Html代碼之後如下:

<div class="modal-bg"></div><div class="modal" style="position: absolute; top: 270px; left: 540px;"><div class="title"><div class="tips-bg"></div><a class="tips">I am a Chinese.</a><a class="close"></a></div><div><img width="600" height="400" src="img/demopage/image-1.jpg"></div></div>

其實是建立了一個模態層來顯示放大圖片。


上面,涉及到的CSS樣式內容如下:

    <style type="text/css">        html, body, .modal-bg {            height: 100%;            margin: 0;            padding: 0;            font-size: 13px;            font-weight: bold;            color: #fff;        }        .modal-bg{            position: absolute;            left: 0px;            top: 0px;            width: 100%;            background: #48525e;            opacity: 0.4;            z-index: 10;        }        .modal{            position: relative;            z-index: 99;            opacity: 1;            top: 15%;            left: 40%;            width: 600px;            height: 400px;        }        .modal .title .tips-bg{            position: absolute;            bottom: 0px;            left: 0px;            background: #48525e;            width: 100%;            height: 50px;            opacity: 0.8;        }        .modal .title .tips{            position: absolute;            bottom: 13px;            left: 10px;            font-family: "Arial";            font-weight: bold;            font-size: 20px;        }        .modal .title .close{            background: url(img/close.png) no-repeat;            width: 27px;            height: 27px;            position: absolute;            top: 5px;            right: 5px;        }        .modal .title .close:hover{            cursor: pointer;        }        .container{            position: absolute;            top: 200px;            text-align: center;            width: 100%;            z-index: 5;        }        .image-list{            width:300px;            margin-left: 40%;            position: relative;        }        #prev{            display: none;            position: absolute;            top: 55px;            left: 35px;            float: left;            width: 45px;            height: 50px;            background: url(img/prev.png);        }        #next{            position: absolute;            top: 55px;            right: 40px;            width: 45px;            height: 50px;            background: url(img/next.png);        }        #prev:hover,#next:hover{            cursor: pointer;        }        .thumb-a{            display:inline-block;            padding:4px;            margin:0 0.5rem 1rem 0.5rem 1rem;            line-height:0;            -webkit-transition:background-color 0.1s ease-out;            -moz-transition:background-color 0.1s ease-out;            -o-transition:background-color 0.1s ease-out;            transition:background-color 0.1s ease-out;            -webkit-border-radius:6px;            -moz-border-radius:6px;            -ms-border-radius:6px;            -o-border-radius:6px;            border-radius:6px        }        .thumb-a:hover{            background-color:#4ae;            cursor: pointer;        }        .thumb-a-hide{            display: none;        }        .thumb-img{            -webkit-border-radius:5px;            -moz-border-radius:5px;            -ms-border-radius:5px;            -o-border-radius:5px;            border-radius:5px        }    </style>

至此,圖片集顯示就完成了,完整html代碼如下:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Image List</title>    <style type="text/css">        html, body, .modal-bg {            height: 100%;            margin: 0;            padding: 0;            font-size: 13px;            font-weight: bold;            color: #fff;        }        .modal-bg{            position: absolute;            left: 0px;            top: 0px;            width: 100%;            background: #48525e;            opacity: 0.4;            z-index: 10;        }        .modal{            position: relative;            z-index: 99;            opacity: 1;            top: 15%;            left: 40%;            width: 600px;            height: 400px;        }        .modal .title .tips-bg{            position: absolute;            bottom: 0px;            left: 0px;            background: #48525e;            width: 100%;            height: 50px;            opacity: 0.8;        }        .modal .title .tips{            position: absolute;            bottom: 13px;            left: 10px;            font-family: "Arial";            font-weight: bold;            font-size: 20px;        }        .modal .title .close{            background: url(img/close.png) no-repeat;            width: 27px;            height: 27px;            position: absolute;            top: 5px;            right: 5px;        }        .modal .title .close:hover{            cursor: pointer;        }        .container{            position: absolute;            top: 200px;            text-align: center;            width: 100%;            z-index: 5;        }        .image-list{            width:300px;            margin-left: 40%;            position: relative;        }        #prev{            display: none;            position: absolute;            top: 55px;            left: 35px;            float: left;            width: 45px;            height: 50px;            background: url(img/prev.png);        }        #next{            position: absolute;            top: 55px;            right: 40px;            width: 45px;            height: 50px;            background: url(img/next.png);        }        #prev:hover,#next:hover{            cursor: pointer;        }        .thumb-a{            display:inline-block;            padding:4px;            margin:0 0.5rem 1rem 0.5rem 1rem;            line-height:0;            -webkit-transition:background-color 0.1s ease-out;            -moz-transition:background-color 0.1s ease-out;            -o-transition:background-color 0.1s ease-out;            transition:background-color 0.1s ease-out;            -webkit-border-radius:6px;            -moz-border-radius:6px;            -ms-border-radius:6px;            -o-border-radius:6px;            border-radius:6px        }        .thumb-a:hover{            background-color:#4ae;            cursor: pointer;        }        .thumb-a-hide{            display: none;        }        .thumb-img{            -webkit-border-radius:5px;            -moz-border-radius:5px;            -ms-border-radius:5px;            -o-border-radius:5px;            border-radius:5px        }    </style>    <script src="js/jquery-1.8.3.js"></script>    <script>        var imgIndex = 1;        function showImg(index){            var width=600,height=400;            var winWidth = $(window).width(),winHeight = $(window).height();            var modalBg = $("<div></div>");            modalBg.addClass("modal-bg");            modalBg.appendTo($('body'));            var modal = $("<div></div>");            modal.addClass("modal");            modal.css("position","absolute")                 .css("top",(winHeight-height)/2+"px")                 .css("left",(winWidth-width)/2+"px");            var titleTipsBg = $("<div></div>").addClass("tips-bg");            var titleTips = $("<a></a>").addClass("tips").html("I am a Chinese.");            var titleClose =  $("<a></a>").addClass("close");            var title = $("<div></div>");            title.addClass("title");            title.append(titleTipsBg)                 .append(titleTips)                 .append(titleClose);            titleClose.on("click",function(){                modalBg.hide();                modal.hide();            });            title.appendTo(modal);            var img = $("<img>").attr("width",width)                                 .attr("height",height)                                 .attr("src","img/demopage/image-"+index+".jpg");            var imgDiv = $("<div></div>").append(img);            imgDiv.appendTo(modal);            modal.appendTo($('body'));        }        function updateImage(type){            if(type==="prev"){                if(imgIndex>1){                    imgIndex=imgIndex-1;                }            }            else{                if(imgIndex<3){                    imgIndex=imgIndex+1;                }            }            for(var i=0;i<3;i++){                $("#img"+(i+1)).addClass("thumb-a-hide");            }            $("#img"+imgIndex).removeClass("thumb-a-hide");            if(imgIndex===3){                $("#next").hide();            }            else{                $("#next").show();            }            if(imgIndex===1){                $("#prev").hide();            }            else{                $("#prev").show();            }        };    </script></head><body><div class="container">    <div class="image-list">        <div id="prev" onclick="updateImage('prev')"></div>        <a id="img1" class="thumb-a" onclick="showImg(1)">            <img class="thumb-img" width="200" height="150" src="img/demopage/thumb-1.jpg" alt="" />        </a>        <a  id="img2" class="thumb-a thumb-a-hide" onclick="showImg(2)">            <img class="thumb-img" width="200" height="150" src="img/demopage/thumb-2.jpg" alt="" />        </a>        <a  id="img3" class="thumb-a thumb-a-hide" onclick="showImg(3)">            <img class="thumb-img" width="200" height="150" src="img/demopage/thumb-3.jpg" alt="" />        </a>        <div id="next"  onclick="updateImage('next')"></div>    </div></div></body></html>

如有疑問請聯絡:

QQ:1004740957

Emai:[email protected]


CSS+JS實現圖片集展示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.