最近接了個項目,項目需求是這樣的,當點擊圖片查看圖片,再次點擊大圖被隱藏,在移動端用的比較多,因為移動端屏幕小,需要查看大圖。具體代碼實現(xiàn)過程本文給大家介紹,需要的朋友可以參考下
一、需求
點擊圖片查看大圖,再點大圖隱藏。多用于移動端,因為移動端屏幕小,可能需要查看大圖。
二、代碼
- <!DOCTYPE html>
- <html>
- <meta charset="utf-8"/>
- <head runat="server">
- <title>JQuery點擊圖片查看大圖by starof</title>
- <style type="text/css">
- .exampleImg { height:100px; cursor:pointer;}
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- <script type="text/javascript">
- //alert($);
- // (function (window, undefined) {
- // var MyJQuery = function () {
- // window.MyjQuery = window.$ = jQuery; window.$ = MyJQuery;
- // };
- // })(window);
- // alert($);
- $.fn.ImgZoomIn = function () {
- bgstr = '<div id="ImgZoomInBG" style=" background:#000000; filter:Alpha(Opacity=70); opacity:0.7; position:fixed; left:0; top:0; z-index:10000; width:100%; height:100%; display:none;"><iframe src="about:blank" frameborder="5px" scrolling="yes" style="width:100%; height:100%;"></iframe></div>';
- //alert($(this).attr('src'));
- imgstr = '<img id="ImgZoomInImage" src="' + $(this).attr('src')+'" onclick=$(/'#ImgZoomInImage/').hide();$(/'#ImgZoomInBG/').hide(); style="cursor:pointer; display:none; position:absolute; z-index:10001;" />';
- if ($('#ImgZoomInBG').length < 1) {
- $('body').append(bgstr);
- }
- if ($('#ImgZoomInImage').length < 1) {
- $('body').append(imgstr);
- }
- else {
- $('#ImgZoomInImage').attr('src', $(this).attr('src'));
- }
- //alert($(window).scrollLeft());
- //alert( $(window).scrollTop());
- $('#ImgZoomInImage').css('left', $(window).scrollLeft() + ($(window).width() - $('#ImgZoomInImage').width()) / 2);
- $('#ImgZoomInImage').css('top', $(window).scrollTop() + ($(window).height() - $('#ImgZoomInImage').height()) / 2);
- $('#ImgZoomInBG').show();
- $('#ImgZoomInImage').show();
- };
- $(document).ready(function () {
- $("#imgTest").bind("click", function () {
- $(this).ImgZoomIn();
- });
- });
- </script>
- </head>
- <body>
- <div>
- <!--第一種寫法-->
- <img class="exampleImg" src="images/03.jpg" id="imgTest"/>
- <!--第二種寫法-->
- <img class="exampleImg" src="images/p1_nav2.png" onClick="$(this).ImgZoomIn();"/>
- </div>
- </body>
- </html>
三、技巧
因為移動端無法添加熱點,最終一個解決方法是使用四個a標簽定位到左上角,右上角,左下角,右下角四個區(qū)域。
- <dl>
- <dd style="display:block;">
- <img src="images/four-duche.jpg" onClick="$(this).ImgZoomIn();">
- <a href="javascript:;" src="images/11.jpg" class="topleft" onClick="$(this).ImgZoomIn();"></a>
- <a href="javascript:;" src="images/12.jpg" class="topright" onClick="$(this).ImgZoomIn();"></a>
- <a href="javascript:;" src="images/13.jpg" class="bottomleft" onClick="$(this).ImgZoomIn();"></a>
- <a href="javascript:;" src="images/14.jpg" class="bottomright" onClick="$(this).ImgZoomIn();"></a>
- </dd>
- ...
- </dl>
- css
- .topleft,.topright,.bottomleft,.bottomright{
- width:50%;
- height:50%;
- position:absolute;
- }
- .topleft{
- /*background-color:red;*/
- top:0;
- left:0;
- }
- .topright{
- /*background-color:green;*/
- top:0;
- right:0;
- }
- .bottomleft{
- /*background-color:blue;*/
- bottom:0;
- left:0;
- }
- .bottomright{
- /*background-color:yellow;*/
- bottom:0;
- right:0;
- }
PS:手機網(wǎng)站移動端圖片實現(xiàn)延遲加載
由于國內(nèi)的電信網(wǎng)絡(luò)性價比的限制,和手機處理能力的差異,在設(shè)計一個無線應(yīng)用的時候,
為用戶節(jié)省流量是一個非常重要的考慮因素。可以說每一個字節(jié)都應(yīng)該為客戶端節(jié)省。
節(jié)約流量可以從以下幾個方面關(guān)注:
一、使用緩存 比如 利用瀏覽器本地存儲 前面已經(jīng)討論過
二、延遲加載代碼 (觸底檢測,通過接口獲取數(shù)據(jù))
三、資源的延遲加載,圖片出現(xiàn)在可視區(qū)域再加載,(不考慮自動播放的情況下)音頻視頻按用戶點擊加載。
今天簡單說一下圖片延遲加載的實現(xiàn)方式。
例子基于jQuery 和 jQuery mobile
原理:用戶滑動屏幕,屏幕滾動結(jié)束(用jQuery 提供的 window scrollstop 事件合適 ) 檢測出現(xiàn)在viewport中的圖片。
替換圖片的 真正 src 屬性即可。
技巧:滾動結(jié)束之后不要立即檢測加載,設(shè)置一秒延時,也許用戶會立即開始下一次滾屏,基于現(xiàn)在的網(wǎng)絡(luò)環(huán)境,1秒的延時可以說明用戶真正想查看這些內(nèi)容。用微信的朋友可以仔細體驗一下這一點。
由于有時鐘的控制,當用戶頻繁快速翻動屏幕,不會發(fā)大量請求。
主要代碼:
- var refreshTimer = null,
- mebook = mebook || {};
- /*
- *滾動結(jié)束 屏幕靜止一秒后檢測哪些圖片出現(xiàn)在viewport中
- *和PC端不同 由于無線速度限制 和手機運算能力的差異 1秒鐘的延遲對手機端的用戶來說可以忍受
- */
- $(window).on('scrollstop', function () {
- if (refreshTimer) {
- clearTimeout(refreshTimer);
- refreshTimer = null;
- }
- refreshTimer = setTimeout(refreshAll, 1e3);
- });
- $.belowthefold = function (element) {
- var fold = $(window).height() + $(window).scrollTop();
- return fold <= $(element).offset().top;
- };
- $.abovethetop = function (element) {
- var top = $(window).scrollTop();
- return top >= $(element).offset().top + $(element).height();
- };
- /*
- *判斷元素是否出現(xiàn)在viewport中 依賴于上兩個擴展方法
- */
- $.inViewport = function (element) {
- return !$.belowthefold(element) && !$.abovethetop(element)
- };
- mebook.getInViewportList = function () {
- var list = $('#bookList li'),
- ret = [];
- list.each(function (i) {
- var li = list.eq(i);
- if ($.inViewport(li)) {
- mebook.loadImg(li);
- }
- });
- };
- mebook.loadImg = function (li) {
- if (li.find('img[_src]').length) {
- var img = li.find('img[_src]'),
- src = img.attr('_src');
- img.attr('src', src).load(function () {
- img.removeAttr('_src');
- });
- }
- };
新聞熱點
疑難解答
圖片精選