Implement a swiper picture switch that fits in the middle of a mobile page

Source: Internet
Author: User

About swiper Picture switch There are many types, I do not introduce each, you can directly to the official website query, today to write a suitable for the middle of the page image switch, but also suitable for any image size (that is, this picture switch does not limit the size of the picture itself, In particular, the mobile side of the dynamically acquired images may not be the same size, so regardless of any picture if you want to keep it in the middle of the mobile page, then you have to get to the image of the Tstyle style, well, not to say, to see the code, since it is the swiper picture switch, Then first introduce Swiper CSS and JS:

<script type="text/javascript" src="js/swiper.min.js " ></script> <link rel="stylesheet" href="css/swiper.min.css  ">

These two are not few, to write to the top of

<style> *{margin:0;padding:0;}Body{font-size:14px;-webkit-text-size-adjust:100%;}. Swiper-container{width:100%;Height:100%; }. Swiper-slide{text-align:Center;font-size:18px;Background-color:#000;Display:-webkit-box;Display:-moz-box;Display:-ms-flexbox;Display:-webkit-flexbox;Display:-moz-flexbox;Display:-webkit-flex;Display:-moz-flex;Display:Flex;-webkit-box-pack:Center;-moz-box-pack:Center;-ms-box-pack:Center;-moz-flex-pack:Center;-ms-flex-pack:Center;-webkit-flex-pack:Center;-webkit-justify-content:Center;-moz-justify-content:Center;justify-content:Center;-webkit-justify-content:Center;-webkit-box-align:Center;-moz-box-align:Center;-ms-flex-align:Center;-webkit-align-items:Center;-moz-align-items:Center;Align-items:Center; }. swiper-slide img{width:100%;Height:100%;}. swiper-slide. Title{position:Absolute;Bottom:0; Left:0;width:100%;Height:35px;Z-index:999}. swiper-slide. Title P{position:Absolute;Line-height:35px;Color:#fff;Z-index:9999; Left:10px;}. Swiper-slide. FL-BG{width:100%;Height:100%;position:Absolute;Top:0; Left:0;Background-color:#000;Opacity:0.6;}. Swiper-pagination{position:fixed;Color:#fff;}. Slide-info{width:100%;Height:Auto;position:Absolute;Top:50%; Left:0;}</style>

I write inline CSS directly, and then look at the layout of Swiper:

<!--requirements: The picture is in the middle of the mobile page switch -<Divclass= "Swiper-container swiper-container-horizontal">    <Divclass= "Swiper-wrapper">        <Divclass= "Swiper-slide">            <Divclass= "Slide-info">                <imgsrc= "Img/pic1.jpg"alt="" />                <Divclass= "title">                    <P>Picture One</P>                    <Divclass= "FL-BG" ></Div>                </Div>            </Div>        </Div>        <Divclass= "Swiper-slide">            <Divclass= "Slide-info">                <imgsrc= "Img/pic2.jpg"alt="" />                <Divclass= "title">                    <P>Picture Two</P>                    <Divclass= "FL-BG" ></Div>                </Div>            </Div>        </Div>        <Divclass= "Swiper-slide">            <Divclass= "Slide-info">                <imgsrc= "Img/pic3.jpg"alt="" />                <Divclass= "title">                    <P>Picture Three</P>                    <Divclass= "FL-BG" ></Div>                </Div>            </Div>        </Div>        <Divclass= "Swiper-slide">            <Divclass= "Slide-info">                <imgsrc= "Img/pic4.jpg"alt="" />                <Divclass= "title">                    <P>Picture One</P>                    <Divclass= "FL-BG" ></Div>                </Div>            </Div>        </Div>    </Div>    <!--If you need a page splitter -    <Divclass= "Swiper-pagination swiper-pagination-fraction"></Div></Div>

Next is the Swiper must write the JS effect:

<script>//Here are some of the Swiper's own JS that must be written.    varSwiper =NewSwiper ('. Swiper-container '), {pagination: {el:'. Swiper-pagination ', type:' Fraction ',}, navigation: {nextEl:'. Swiper-button-next ', Prevel:'. Swiper-button-prev ',        },    }); //the picture is in the middle of the page JS    //compatible with different browsers to get the inline style    functionGetStyle (obj, attr) {returnObj.currentstyle?obj.currentstyle[attr]: getComputedStyle (obj) [attr]; } window.onload=function(){        varWinh = document.body.scrollheight==0?Document.documentElement.scrollHeight:document.body.scrollHeight; varPich=document.getelementsbyclassname ("Swiper-slide");  for(vari=0;i<pich.length;i++){            varDivh=pich[i].getelementsbyclassname ("Slide-info") [0]; varOimg=pich[i].getelementsbytagname ("img") [0]; Pich[i].index=i; Pich[i].style.height=winh+ "px"; DivH.style.height= GetStyle (oimg, ' height ')); DivH.style.marginTop=-parsefloat (GetStyle (oimg, ' height '))/2+ "px";        }    }</script>

OK, so simply write, the effect will appear, to see the simple implementation diagram:

To achieve this small example, several core points are:

1, to adapt to a variety of mobile phones, so to know the scrollheight of each page;

2, the picture size is different, but also to calculate the height of each picture, and then let it be positioned in the center of the page.

This is just a simple small example, but the mobile side is likely to be used to, hope to help everyone!

Implement a swiper picture switch that fits in the middle of a mobile page

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.