仿iPhone、iPad界面滑屏切换

<!DOCTYPE html>
<html lange='en'>

<head>
    <meta charset='UTF-8'>
    <title></title>
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <style>
        * {
        margin: 0;
        padding: 0;
    }

    #div1 {
        width: 670px;
        height: 250px;
        position: relative;
        margin: 50px;
        overflow: hidden;
        background: pink;
    }

    #ul1 {
        position: absolute;
        left: 0;       
    }

    #ul1 li {
        width: 670px;
        height: 250px;
        float: left;
        list-style: none;
    }
     #ul1 li img{
         width: 670px;
     }
    </style>
</head>

<body>
    <div id="div1">
        <ul id="ul1">
            <li><img src="img/1.jpg"></li>
            <li><img src="img/2.jpg"></li>
            <li><img src="img/3.jpg"></li>
            <li><img src="img/4.jpg"></li>
            <li><img src="img/5.jpg"></li>
        </ul>
    </div>
</body>

</html>
<script type="text/javascript">
var oDiv = document.getElementById('div1');
var oUl = document.getElementById('ul1');
var aLi = oUl.getElementsByTagName('li');
var w = aLi[0].offsetWidth;

oUl.style.width = aLi.length * w + 'px';
var downLeft = 0;
var donwX = 0;
var iNow = 0;
var downTime = 0;
var bBtn;

oUl.ontouchstart = function(ev) {
    console.log(1)
    var touchs = ev.changedTouches[0];
    donwX = touchs.pageX;
    downLeft = this.offsetLeft;
    bBtn = true;
    downTime = Date.now();
}

// console.log(donwX)
oUl.ontouchmove = function(ev) {
    var touchs = ev.changedTouches[0];
    // console.log(touchs.pageX,donwX)
    // console.log(this.offsetLeft)
    // console.log(touchs.pageX)
    if (this.offsetLeft >= 0) {
        // console.log(1111111)
        if (bBtn) {
            donwX = touchs.pageX;
            bBtn = false;
        }
        this.style.left = (touchs.pageX - donwX) / 3 + 'px';
        console.log(1)

    } else if (this.offsetLeft <= oDiv.offsetWidth - oUl.offsetWidth) {
        if (bBtn) {
            donwX = touchs.pageX;
            bBtn = false;
        }
        this.style.left = (touchs.pageX - donwX) / 3 + (oDiv.offsetWidth - oUl.offsetWidth) + 'px';
        console.log(2)

    } else {
        // debugger;
        this.style.left = touchs.pageX - donwX + downLeft + 'px';
        // console.log(touchs.pageX - donwX + downLeft+'this is')
        console.log(3)

    }
    // console.log(this.style.left);
}

oUl.ontouchend = function(ev) {
    var touchs = ev.changedTouches[0];

    if (touchs.pageX < donwX) { //左

        if (iNow < aLi.length - 1) {
            // alert(Date.now() - downTime<300)
            if (donwX - touchs.pageX > aLi[0].offsetWidth / 2 || (Date.now() - downTime < 300 && donwX - touchs.pageX > 30)) {

                iNow++;
            }
        }

        $('#ul1').animate({ 'left': -iNow * w });
    } else { //右
        // debugger;
        if (iNow > 0) {
            if (touchs.pageX - donwX > aLi[0].offsetWidth / 2 || (Date.now() - downTime < 300 && touchs.pageX - donwX > 30)) {
                iNow--;
            }
        }
        $('#ul1').animate({ 'left': -iNow * w });

    }
}
</script>
上一篇:【Python训练营】Python每日一练----第26天:后缀表达式


下一篇:*****又错一道,不知道为啥。。。 ybt【例5.19】字符串判等