Home >Web Front-end >JS Tutorial >Based on JavaScript, click on the image on the mobile terminal to view the larger image. Click on the larger image to hide_javascript skills
1. Demand
Click on the image to view the larger image, and then click on the larger image to hide it. It is mostly used on mobile devices because the mobile screen is small and you may need to view large images.
2. Code
<!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>
3. Skills
Because the mobile terminal cannot add hotspots, the final solution is to use four a tags to locate the upper left corner, upper right corner, lower left corner, and lower right corner.
<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: Mobile website mobile images implement delayed loading
Due to the limitations of the cost-effectiveness of domestic telecommunications networks and the differences in mobile phone processing capabilities, when designing a wireless application,
Saving traffic for users is a very important consideration. Arguably every byte should be saved for the client.
Saving traffic can focus on the following aspects:
1. Use cache, such as using browser local storage, which has been discussed before
2. Delay loading code (bottom detection, obtaining data through interface)
3. Delayed loading of resources, pictures appear in the visible area and then loaded, (without considering automatic playback) audio and video are loaded according to user clicks.
Today I will briefly talk about how to implement delayed loading of images.
Examples based on jQuery and jQuery mobile
Principle: The user slides the screen and the screen scrolling ends (use the window scrollstop event provided by jQuery appropriately) to detect the picture that appears in the viewport.
Just replace the real src attribute of the image.
Tip: Do not detect loading immediately after scrolling. Set a one-second delay. Maybe the user will immediately start the next scroll. Based on the current network environment, a 1-second delay can indicate that the user really wants to view the content. Friends who use WeChat can experience this carefully.
Due to clock control, when the user frequently and quickly flips the screen, a large number of requests will not be issued.
Main code:
var refreshTimer = null, mebook = mebook || {}; /* *滚动结束 屏幕静止一秒后检测哪些图片出现在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(); }; /* *判断元素是否出现在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'); }); } };