Mobile js Image Viewer and js Image Viewer

Source: Internet
Author: User

Mobile js Image Viewer and js Image Viewer

This article provides examples to share with you how to use the js Image Viewer plug-in and how to create a webpage Image Viewer for your reference. The specific content is as follows:

The development of a custom image Viewer for the project has been completed in the past few days.

Development scenario: In a multi-File Download display list, if some files are detected as images, the image Viewer opens when you click the file to display the image, other images in the list are displayed in the viewer queue at the same time, which can be viewed by swiping before and after, and other ancillary functions.

At first glance, the features seem to be a little more complicated and need to be sorted out.

Functions

First, we need to obtain the clicked image file object and the matching image file object set.

Secondly, Image Viewer creation and image queue display

Then, the image-friendly Loading Method

Finally, the implementation of the image viewer's touch sliding and sliding Functions

It seems that there are not many

Create a mobile webpage Image Viewer

Prepare for formal development based on functional points

First, we add a unified identifier for image files in a known list container. Whether the file is an image or image path is known during storage. We directly add a unified attribute for the element.

<A url = "..."> </a>

Second, create a full-screen gray background, display the image queue, and use NO. the position of the currently displayed image is marked in the form of/n. If all the styles are provided, they are not described in detail (some of the attributes in the figure style are swipe. js required)

.dialog,.dialog figure{position:fixed;top:0;bottom:0;left:0;right:0;z-index:1001;}.dialog section{height:100%;background:#333;-webkit-opacity:0.7;}.dialog footer{padding:10px;position:absolute;bottom:0;left:0;right:0;z-index:1002;font-size:13px;}.dialog footer span{padding:0 20px;float:right;border:1px solid #999;border-radius:15px;color:#ddd;}.dialog figure{overflow:hidden;}.dialog figure ul{height:100%;overflow:hidden;}.dialog figure li{width:100%;height:100%;display:-webkit-box;float:left;position:relative;-webkit-box-pack:center;-webkit-box-align:center;}.dialog figure img{max-width:100%;max-height:100%;margin:10px;}

Then, convert the image into a loading.gif image during regionalization, and then dynamically load the image.


Finally: swipe. js lightweight touch slide plug-in learning to use, first call

var obj = document.getElementById('swipe');window.mySwipe = Swipe(obj, { ...});

Configuration parameters

StartSlide: 0, // start position auto: 3000, // automatic playback time continuous: true, // infinite loop; I suggest assigning a value of false if the number of all items is unknown, otherwise, 2 disableScroll: false, // call back: function (index, element) {} is not allowed during touch. // The callback function is used when sliding, the parameters are sliding element sorting and object transitionEnd: function (index, element) {}// callback function after sliding is completed. The parameters are the same as above

API method

Prev (); // previous page next (); // next page getPos (); // current page index getNumSlides (); // The number of all items slide (index, duration ); // sliding effect

Basic html Structure

<figure id="swipe"> <ul>  <li></li> </ul></figure>

Required css attributes

figure{overflow:hidden;position:relative;}figure ul{overflow:hidden;}figure li{width:100%;float:left;position:relative;}

Simple and Easy to use regardless of the size or documentation

Download swipe. min. js

Complete development officially started

We use the unified trigger event in the list to obtain the url attribute of the trigger object. If this attribute exists, we call the Image Viewer and stop the program on the download page.

$('.download a').click(function(){ var obj = $(this); var url = obj.attr('url'); if(url && url.length > 0){  var set = $('.download a[url]');  base_module.dialog(obj, set);  return false; }; ...});

Now, after data collection is complete, we will first display our object model, the attributes of the object model, and the definition rules of the methods.

var base_module = (function(){ var base = {}; base.options = {  LOCK : false }; base.fn = function(){  ... }; return base;})();

Compile the main function of the Image Viewer

/* ** Image Viewer * @ param object obj operation object * @ param object set */base. dialog = function (obj, set) {var I = set. index (obj); // index var rel = set. length; // Number of all items var html = '<section class = "diipe"> <section> </section> <figure id = "swipe"> <ul> '; // start to draw the Image Viewer set. each (function () {var url = $ (this ). attr ('url '); // image path: html + = '<li>  </li> '; // draw the image list cyclically}); html + = '</ul> </figure> <footer> <span id = "po">' + (I + 1) + '/' + rel + '</span> </footer> </section>'; // draw end $ ('body '). append (html); // render the Image Viewer // js file loading status base. loadJs ('swipe. min. js', function () {base. swiper (I); // callback function, swipe parameter configuration}); var url = obj. attr ('url'); // The base of the image loading status. loadImg (url, function () {base. imager (I); // callback function, Image Display });};

Load swipe. js as needed

/*** Load js as needed * @ param string url file path * @ param object fn callback function */base. loadJs = function (url, fn) {if (typeof Swipe! = 'Undefined') {if (fn) fn (); return false ;}; var js = document. createElement ('script'); js. src = url; document. getElementsByTagName ('head') [0]. appendChild (js); js. onload = function () {if (fn) fn ();};};

Configure swipe. js Parameters

/*** Slide configuration * @ param int I current page index */base. swiper = function (I) {var obj = document. getElementById ('swip'); window. mySwipe = Swipe (obj, {startSlide: I, continuous: false, disableScroll: true, callback: function (index, element) {var I = index + 1; var s = $ ('# swipe li '). length; $ ('# po '). text (I + '/' + s); var url = $ (element ). find ('img '). attr ('url'); base. loadImg (url, function () {base. imager (index );});}});};

Attach images as needed

/*** Load img as needed * @ param string url file path * @ param object fn callback function */base. loadImg = function (url, fn) {var img = new Image (); img. src = url; if (img. complete) {if (fn) fn (); return false ;}; img. onload = function () {if (fn) fn ();};};

After the image is loaded, it is displayed.

/*** Display the loaded image * @ param int I current page index */base. imager = function (I) {var obj = $ ('# swipe li '). eq (I ). find ('img '); var url = obj. attr ('url'); obj. replaceWith (' ');};

At present, it is only preliminary work, and we still needOptimized, Mainly including the following points

The image viewer has been drawn successfully. When it is closed, it should not be deleted but hidden. When the viewer is called again, the list of parts does not change, and the image does not need to be re-drawn;

The image cannot be enlarged or reduced. If the width and height are too long, the preview effect will be poor and the image cannot be viewed;

Without thumbnails, we found that we did not compress images when storing images during development. The traffic when loading images is too bad. Of course, this problem must be solved first in the background storage.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.