Description of the project:
A period of time ago the mobile web site requires the implementation of a "content touch swipe" function, the requirements are as follows:
1. Display the large image by clicking on the small map;
2. Click the large image again to return to the original small image;
3. The image can be slid and switched;
4. Displays the index of the current picture (which picture I am currently viewing).
Wrote some of their own functions, completed the 1th, 2 of the demand, but because of the poor JS Foundation, borrowed Swiper this plug-in to complete the remaining two requirements.
Swiper is often used for content-touch swipe on mobile websites. Swiper can realize touch screen focus map, touch screen tab switch, touch screen multi-graph switch and other common effects. Swiper Official website http://www.swiper.com.cn/, there is a detailed API documentation and content of the complete online presentation.
First, show me the demo structure:
This is an activity silhouette category, which shows a total of three "items (LI)", each of which has multiple list items to wrap img display images.
Problem-Solving ideas:
1. Since only a maximum of nine images are required for each project, extra images are removed (this feature is also implemented in this article);
2. Since there are multiple Silhouette-item, when we click on one of the images in this project, we can create a container at the end of each silhouette-item to hold a container for the large image to be displayed;
Workaround:
1. For HTML, the code is a bit long, from the above demo structure can be seen in my HTML code,
2. For CSS, the code is a bit long, but there is nothing particularly important to point out, see one of the following list items (Silhouette-item) The final display effect is enough:
3. JS Code:
/*= = = Limit the number of pictures of the activity silhouette, max = = 9*/ for(vari = 0, Listlen = $ (". Silhouette-img-list"). Length; i < Listlen; i++) { var$lis = $ (". Silhouette-img-list"). Find ("Li")); varLilen = $ (". Silhouette-img-list"). EQ (i). Find ("Li"). length; if(Lilen > 9) { for(varj = 9; J < Lilen; J + +) {$ ($lis [j]). Remove ();//the value of Listlen will change after each deletion } }}
Description of the code details:
1. Because each list item (Silhouette-item) has a UL element of class silhouette-img-list, the outer layer defines a for loop to find the UL element in which list item is specific;
2. In the third row. The purpose of EQ (i) is to locate the specific UL.
3. after each deletion, the value of Listlen will change, so the value of Listlen must be determined first, not in the inner loop as a judgment. For example, we have 14 pictures, we need to delete 5, if Listlen not as a fixed value to judge, but each time the inner layer loop later comparison, then the number of comparisons will be less, the first comparison 9 < 14, the second comparison < 13, the third comparison < 12. Only three loops will be executed, which will reduce the deletion of 2 images and not conform to our expectations.
/*= = = Activity silhouette, click the picture Full screen display, can slide toggle = = =*/$(". Silhouette-img-list li img"). Click (function() { //get the number of pictures in an active silhouette list varIMG = $ ( This). Parents (". Silhouette-img-list"). FIND ("img")); varImglen =img.length; //the width of the popup layer is the effective width available to the browser, and the height is the available effective height of the browser varW =window.innerwidth; varh =Window.innerheight; //gets the distance of the current position of the page from the top of the window vartop = Document.body.scrollTop | |Document.documentElement.scrollTop; //Create a Swiper-container box with a large map var$container = $ (' <div class= "Swiper-container" ></div> "); //pagination setting up the paging device var$pagination = $ (' <div class= "swiper-pagination" ></div> "); var$wrapper = $ (' <ul class= "Swiper-wrapper" ></ul> "); //item is used to store a list of pictures, emulate the HTML DOM, and use string connections varItem = ""; //The structure of the silhouette-img-list part of the HTML is implemented with a for loop, and the src and ALT attribute values are the same as those for each large map setting and corresponding small plots. for(vari = 0; i < Imglen; i++) { varsrc = $ (img[i]). attr ("src"); varalt = $ (img[i]). attr ("Alt");
//<strong class= "Tips" > click again to return </strong> add to the back, not necessary (generally know!) ) Item+ = "<li class= ' swiper-slide ' ><strong class= ' tips ' &G t; Click the big picture again to return to </strong></li>; } //inserting child elements into a parent element$ ($wrapper). Append (item); $ ($container). prepend ($wrapper); $ ($container). Append ($pagination); $( This). Parents (". Silhouette-item"). Append ($container); //under face Swiper plug-in reference varMyswiper =NewSwiper (". Swiper-container", {direction:"Horizontal",//Slide HorizontallyGrabcursor:true,//the pointer changes to the Palm shape when the mouse overrides the Swiper, and the pointer changes to the gripper shape when you drag //Loop:true,//True when sliding to the first and last sheet can still slideObservertrue,//automatic initialization of swiper when modifying swiper itself or child elementsObserveparents:true,//automatically initializes swiper when modifying the parent element of a swiperInitialslide: $ ( This). Parent (). Index (),//set the index of slide at initialization timePagination: ". Swiper-pagination"//Page Splitter}); //styling a picture in a matte layer and mask layer$ (". Swiper-container"). css ({position:"Absolute", Top:top, //position top so that the mask background of the large image appears in the correct position, not always left at the top of the page:0, Width:w, height:h}); $(". Swiper-slide"). css ({zIndex:99, BackgroundColor:"#000"//This is the background of Li with large map }); $(". Swiper-img"). css ({ //Set the style for the picture, let him center the display, and the width does not exceed the effective width that the browser can display, highly adaptive zIndex:100, display:"Block", Position:"Absolute", Top:0, right:0, bottom:0, left:0, margin:"Auto", MaxWidth:"100%", Height:"Auto" }); $(". Tips"). css ({ //This is the style of hint text, not required, you can omit zIndex:200, display:"Block", Position:"Absolute", bottom:"23px", margin:"Auto", Width:"100%", TextAlign:"Center", Paddingtop:"5px", Paddingbottom:"5px", FontSize:"12px", BackgroundColor:"Rgba (0, 0, 0,. 5)", Color:"#fff" }); //Click Mask layer to delete the previously created node$ (". Swiper-container:not ('. Swiper-pagination ')"). Click (function() { $( This). Remove (); });});
After inserting the node in Firefox display the effect and expected, just down very ugly, not posted up.
Finally, in chrome, the test looks like this:
Content of mobile Web site touch-Swipe