This is a few days to develop a tailor-made picture viewer for the project, which has been initially completed
The development scenario is: In a multi-file download display list, such as the detection of some files as a picture, when you click on the file to open the picture Viewer to display the picture, and the list of other pictures in the viewer queue, can be used to slide the view and other ancillary functions
At first glance the function point seems to be a bit more and more complicated, need to comb
function Point Finishing
First, we want to get a click of the picture file object and the set of eligible picture file objects
Second, the picture Viewer production and picture queue display
Then, picture-friendly loading method
Finally, the image viewer touch sliding and the implementation of the relevant functions after sliding
It's a simple arrangement, not as much as it seems.
make a mobile web image viewerPrepare for formal development based on function points
First: We add a unified identity to the picture file in the known list container, whether the file is a picture or a picture path we are known to add a uniform attribute to the element directly when stored
<url= "..."></a>
Second: Make a full-screen gray background, display the picture queue, and in the form of no./n to mark the current display picture position; All styles are given, and are not discussed in detail (the figure style has a partial attribute of swipe.js must)
. 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:15px;Line-height:30px;}. 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%;padding:10px;Display:-webkit-box;float: Left;position:relative;-webkit-box-pack:Center;-webkit-box-align:Center;}. Dialog Figure img{Max-width:100%;width:40px;Max-height:100%;Height:40px;}
Then: Initialize the picture directly into the Loading.gif picture, and then dynamically load
<src= "loading.gif" url= "..."/>
Finally: Swipe.js lightweight touch Slide plugin learning to use, first call
var obj = document.getElementById (' swipe '= swipe (obj, { ... });
Configuration parameters
Startslide // start position Auto // autoplay time continuous true// Infinite loop false// Touch Disable Roll screen callback function// The callback function when sliding, the parameter is the sliding element sort and the object function// sliding completes the callback function, the parameter ibid
API methods
// Previous Page // Next Page // Current page Index // the number of all items // Slide Effect
Basic HTML structure
<id= "swipe"> <ul> < Li ></ Li > </ ul > </ Figure >
Required CSS Properties
Figure {overflow:hidden; position:relative;} Figure ul {overflow:hidden;} Figure Li {width:100%; float:left; position:relative;}
Very lightweight, easy to get started, no matter the size or the documentation
Swipe.min.js Download
Full-fledged development begins
We get the URL property of the trigger object by the unified trigger event of the list, if the attribute exists, then call the picture Viewer and stop behind the download interface program.
$ ('. Download a '). Click (function() { var obj = $ (this); var url = obj.attr (' url '); if (URL && url.length > 0) { var set = $ ('. Download A[url] '); Base_module.picture (obj, set); return false ; }; ...});
Now into the data collection after the processing process, first show our object model, object model of the properties and methods of the definition of rules
var base_module = (function() { var base = {}; = { false }; function (){ ... }; return Base;}) ();
Writing the picture Viewer main function
/** Image Viewer * @param object obj operand * @param object Set object Set*/base.picture=function(obj, set) {vari = Set.index (obj);//Current page Index varrel = Set.length;//the number of all items varhtml = ' <section class= ' dialog ' ><section></section><figure id= ' swipe ' ><ul> ';//start drawing the picture viewerSet.each (function(){ varURL = $ ( This). attr (' url ');//Picture PathHTML + = ' <li></li> ';//cycle through a list of pictures }); HTML+ = ' </ul></figure><footer><span id= "seat" rel= "' + rel + '" > ' + (i + 1) + '/' + rel + ' </SPAN&G T;</footer></section> ';//Draw End $(' Body '). Append (HTML);//Render Picture Viewer //js file load StatusBase.loadjs (' Swipe.min.js ',function() {base.swiper (i);//callback function, swipe parameter configuration }); varurl = obj.attr (' URL ')); //Picture Loading statusBase.loadimg (URL,function() {Base.imager (i);//callback function, image display });};
Load Swipe.js on Demand
/** * Load js * @param string URL file path * @param object fn callback function*/Base.loadjs=function(URL, fn) {if(typeofSwipe! = ' undefined '){ if(FN) FN (); return false; }; varJS = document.createelement (' script '); Js.setattribute (' SRC ', URL); document.getElementsByTagName (' head ') [0].appendchild (JS); Js.onload=function(){ if(FN) FN (); };};
Configuring the Swipe.js parameter
/** * Slideshow configuration * @param int I sort*/Base.swiper=function(i) {varobj = document.getElementById (' swipe '); Window.myswipe=Swipe (obj, {startslide:i, Disablescroll:true, Callback:function(index, Element) {varobj = $ (' #seat '); vari = index + 1; varrel = obj.attr (' rel '); Obj.text (i+ '/' +rel); varurl = $ (Element). FIND (' img '). attr (' url ')); Base.loadimg (URL,function() {Base.imager (index); }); } });};
Dynamically load pictures on demand
/** * loading img On Demand * @param string URL file path * @param object fn callback function*/base.loadimg=function(URL, fn) {varIMG =NewImage (); IMG.SRC=URL; if(img.complete) {if(FN) FN (); return false; }; Img.onload=function(){ if(FN) FN (); };};
Show when the picture is finished loading
/*/function(i) {var obj = $ (' #swipe li '). EQ (i). Find (' img '); var url = obj.attr (' url '); Obj.attr (' src ', url). css ({' width ': ' Auto ', ' height ': ' Auto '}) ;
At present, it is only preliminary completion, the latter need to be optimized, mainly the following points
The picture Viewer has been drawn successfully, it should not be deleted but hidden when it is closed, and when the viewer is recalled, the list of slices does not change and is directly aroused without redrawing
Picture can not enlarge contraction, wide height is too long, the preview effect is very poor unable to see the picture
Development only to find that we store pictures when there is no compression processing graphics, loading pictures when the traffic is too pit, of course, this problem first to be processed from the background
The picture needs to be saved to the phone album, but the page does not seem to be completed in that case and requires project interface support
Swipe.js plug-in bug
I'll fill in the back of these pits.
Create a Web page image viewer for your phone