国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

基于JavaScript實現移動端點擊圖片查看大圖點擊大圖隱藏

2024-05-06 16:24:55
字體:
來源:轉載
供稿:網友

最近接了個項目,項目需求是這樣的,當點擊圖片查看圖片,再次點擊大圖被隱藏,在移動端用的比較多,因為移動端屏幕小,需要查看大圖。具體代碼實現過程本文給大家介紹,需要的朋友可以參考下

一、需求

點擊圖片查看大圖,再點大圖隱藏。多用于移動端,因為移動端屏幕小,可能需要查看大圖。

二、代碼

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <meta charset="utf-8"/> 
  4. <head runat="server"
  5. <title>JQuery點擊圖片查看大圖by starof</title> 
  6. <style type="text/css"
  7. .exampleImg { height:100px; cursor:pointer;} 
  8. </style> 
  9. <script src="http://code.jquery.com/jquery-latest.js"></script> 
  10. <script type="text/javascript"
  11. //alert($); 
  12. // (function (window, undefined) { 
  13. // var MyJQuery = function () { 
  14. // window.MyjQuery = window.$ = jQuery; window.$ = MyJQuery; 
  15. // }; 
  16. // })(window); 
  17. // alert($); 
  18. $.fn.ImgZoomIn = function () { 
  19. 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>'
  20. //alert($(this).attr('src')); 
  21. imgstr = '<img id="ImgZoomInImage" src="' + $(this).attr('src')+'" onclick=$(/'#ImgZoomInImage/').hide();$(/'#ImgZoomInBG/').hide(); style="cursor:pointer; display:none; position:absolute; z-index:10001;" />'
  22. if ($('#ImgZoomInBG').length < 1) { 
  23. $('body').append(bgstr); 
  24. if ($('#ImgZoomInImage').length < 1) { 
  25. $('body').append(imgstr); 
  26. else { 
  27. $('#ImgZoomInImage').attr('src', $(this).attr('src')); 
  28. //alert($(window).scrollLeft()); 
  29. //alert( $(window).scrollTop()); 
  30. $('#ImgZoomInImage').css('left', $(window).scrollLeft() + ($(window).width() - $('#ImgZoomInImage').width()) / 2); 
  31. $('#ImgZoomInImage').css('top', $(window).scrollTop() + ($(window).height() - $('#ImgZoomInImage').height()) / 2); 
  32. $('#ImgZoomInBG').show(); 
  33. $('#ImgZoomInImage').show(); 
  34. }; 
  35. $(document).ready(function () { 
  36. $("#imgTest").bind("click"function () { 
  37. $(this).ImgZoomIn(); 
  38. }); 
  39. }); 
  40. </script> 
  41. </head> 
  42. <body> 
  43. <div> 
  44. <!--第一種寫法--> 
  45. <img class="exampleImg" src="images/03.jpg" id="imgTest"/> 
  46. <!--第二種寫法--> 
  47. <img class="exampleImg" src="images/p1_nav2.png" onClick="$(this).ImgZoomIn();"/> 
  48. </div> 
  49. </body> 
  50. </html> 

三、技巧

因為移動端無法添加熱點,最終一個解決方法是使用四個a標簽定位到左上角,右上角,左下角,右下角四個區域。

 

 
  1. <dl> 
  2. <dd style="display:block;"
  3. <img src="images/four-duche.jpg" onClick="$(this).ImgZoomIn();"
  4. <a href="javascript:;" src="images/11.jpg" class="topleft" onClick="$(this).ImgZoomIn();"></a> 
  5. <a href="javascript:;" src="images/12.jpg" class="topright" onClick="$(this).ImgZoomIn();"></a> 
  6. <a href="javascript:;" src="images/13.jpg" class="bottomleft" onClick="$(this).ImgZoomIn();"></a> 
  7. <a href="javascript:;" src="images/14.jpg" class="bottomright" onClick="$(this).ImgZoomIn();"></a> 
  8. </dd> 
  9. ... 
  10. </dl> 
  11. css 
  12. .topleft,.topright,.bottomleft,.bottomright{ 
  13. width:50%; 
  14. height:50%; 
  15. position:absolute; 
  16. .topleft{ 
  17. /*background-color:red;*/ 
  18. top:0; 
  19. left:0; 
  20. .topright{ 
  21. /*background-color:green;*/ 
  22. top:0; 
  23. right:0; 
  24. .bottomleft{ 
  25. /*background-color:blue;*/ 
  26. bottom:0; 
  27. left:0; 
  28. .bottomright{ 
  29. /*background-color:yellow;*/ 
  30. bottom:0; 
  31. right:0; 

PS:手機網站移動端圖片實現延遲加載

由于國內的電信網絡性價比的限制,和手機處理能力的差異,在設計一個無線應用的時候,

為用戶節省流量是一個非常重要的考慮因素。可以說每一個字節都應該為客戶端節省。

節約流量可以從以下幾個方面關注:

一、使用緩存 比如 利用瀏覽器本地存儲 前面已經討論過

二、延遲加載代碼 (觸底檢測,通過接口獲取數據)

三、資源的延遲加載,圖片出現在可視區域再加載,(不考慮自動播放的情況下)音頻視頻按用戶點擊加載。

今天簡單說一下圖片延遲加載的實現方式。

例子基于jQuery 和 jQuery mobile

原理:用戶滑動屏幕,屏幕滾動結束(用jQuery 提供的 window scrollstop 事件合適 ) 檢測出現在viewport中的圖片。

替換圖片的 真正 src 屬性即可。

技巧:滾動結束之后不要立即檢測加載,設置一秒延時,也許用戶會立即開始下一次滾屏,基于現在的網絡環境,1秒的延時可以說明用戶真正想查看這些內容。用微信的朋友可以仔細體驗一下這一點。

由于有時鐘的控制,當用戶頻繁快速翻動屏幕,不會發大量請求。

主要代碼:

 

 
  1. var refreshTimer = null
  2. mebook = mebook || {}; 
  3. /* 
  4. *滾動結束 屏幕靜止一秒后檢測哪些圖片出現在viewport中 
  5. *和PC端不同 由于無線速度限制 和手機運算能力的差異 1秒鐘的延遲對手機端的用戶來說可以忍受 
  6. */ 
  7. $(window).on('scrollstop'function () { 
  8. if (refreshTimer) { 
  9. clearTimeout(refreshTimer); 
  10. refreshTimer = null
  11. refreshTimer = setTimeout(refreshAll, 1e3); 
  12. }); 
  13. $.belowthefold = function (element) { 
  14. var fold = $(window).height() + $(window).scrollTop(); 
  15. return fold <= $(element).offset().top; 
  16. }; 
  17. $.abovethetop = function (element) { 
  18. var top = $(window).scrollTop(); 
  19. return top >= $(element).offset().top + $(element).height(); 
  20. }; 
  21. /* 
  22. *判斷元素是否出現在viewport中 依賴于上兩個擴展方法  
  23. */ 
  24. $.inViewport = function (element) { 
  25. return !$.belowthefold(element) && !$.abovethetop(element) 
  26. }; 
  27. mebook.getInViewportList = function () { 
  28. var list = $('#bookList li'), 
  29. ret = []; 
  30. list.each(function (i) { 
  31. var li = list.eq(i); 
  32. if ($.inViewport(li)) { 
  33. mebook.loadImg(li); 
  34. }); 
  35. }; 
  36. mebook.loadImg = function (li) { 
  37. if (li.find('img[_src]').length) { 
  38. var img = li.find('img[_src]'), 
  39. src = img.attr('_src'); 
  40. img.attr('src', src).load(function () { 
  41. img.removeAttr('_src'); 
  42. }); 
  43. }; 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 虎林市| 陆河县| 洛扎县| 乌海市| 柳河县| 淳安县| 苏尼特左旗| 连平县| 鸡东县| 西青区| 揭东县| 文安县| 德清县| 九台市| 惠来县| 安远县| 海安县| 平江县| 阜阳市| 图木舒克市| 灵武市| 罗城| 云浮市| 屏东县| 潼南县| 天祝| 静宁县| 临江市| 淮安市| 兖州市| 宜兴市| 偃师市| 松滋市| 大冶市| 澄江县| 仁寿县| 阿尔山市| 安西县| 英吉沙县| 乌鲁木齐县| 辉县市|