问答详情
源自:3-1 箭头切换

如何使幻灯图片自适应手机

<!DOCTYPE html>

<html>

<head>

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="UTF-8"%>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">

<script src="https://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="https://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>





    <script type="text/javascript">


        window.onload = function () {

            var container = document.getElementById('container');

            var list = document.getElementById('list');

            var buttons = document.getElementById('buttons').getElementsByTagName('span');

            var prev = document.getElementById('prev');

            var next = document.getElementById('next');

            var index = 1;

            var len = 5;

            var animated = false;

            var interval = 3000;

            var timer;



            function animate (offset) {

                if (offset == 0) {

                    return;

                }

                animated = true;

                var time = 300;

                var inteval = 10;

                var speed = offset/(time/inteval);

                var left = parseInt(list.style.left) + offset;


                var go = function (){

                    if ( (speed > 0 && parseInt(list.style.left) < left) || (speed < 0 && parseInt(list.style.left) > left)) {

                        list.style.left = parseInt(list.style.left) + speed + 'px';

                        setTimeout(go, inteval);

                    }

                    else {

                        list.style.left = left + 'px';

                        if(left>-200){

                            list.style.left = -600 * len + 'px';

                        }

                        if(left<(-600 * len)) {

                            list.style.left = '-600px';

                        }

                        animated = false;

                    }

                }

                go();

            }


            function showButton() {

                for (var i = 0; i < buttons.length ; i++) {

                    if( buttons[i].className == 'on'){

                        buttons[i].className = '';

                        break;

                    }

                }

                buttons[index - 1].className = 'on';

            }


            function play() {

                timer = setTimeout(function () {

                    next.onclick();

                    play();

                }, interval);

            }

            function stop() {

                clearTimeout(timer);

            }


            next.onclick = function () {

                if (animated) {

                    return;

                }

                if (index == 5) {

                    index = 1;

                }

                else {

                    index += 1;

                }

                animate(-600);

                showButton();

            }

            prev.onclick = function () {

                if (animated) {

                    return;

                }

                if (index == 1) {

                    index = 5;

                }

                else {

                    index -= 1;

                }

                animate(600);

                showButton();

            }


            for (var i = 0; i < buttons.length; i++) {

                buttons[i].onclick = function () {

                    if (animated) {

                        return;

                    }

                    if(this.className == 'on') {

                        return;

                    }

                    var myIndex = parseInt(this.getAttribute('index'));

                    var offset = -600 * (myIndex - index);


                    animate(offset);

                    index = myIndex;

                    showButton();

                }

            }


            container.onmouseover = stop;

            container.onmouseout = play;


            play();


        }

    </script>


<style type="text/css">

      


        #container {height: 200px; border: 3px solid #333; overflow: hidden;}

        #list { width: 4200px; height: 400px; position: absolute; z-index: 1;}

        #list img { float: left;}

        #buttons { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px;}

        #buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 5px;}

        #buttons .on {  background: orangered;}

        .arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px;  position: absolute; z-index: 2; top: 80px; background-color: RGBA(0,0,0,.3); color: #fff;}

        .arrow:hover { background-color: RGBA(0,0,0,.7);}

        #container:hover .arrow { display: block;}

        #prev { left: 20px;}

        #next { right: 20px;}

    </style>




</head>

<body>


<div data-role="page" id="pageone" data-theme="a">


<div align="center" data-role="header">

<img src="LOGO.png" height="58">

</div>


<div id="container">

    <div  id="list" style="left: -600px; height:200px;">

     

        <img src="5.jpg" alt="1" />

        <img src="1.jpg"  alt="1"/>

        <img src="2.jpg" alt="2" />

        <img src="3.jpg" alt="3" />

        <img src="4.jpg" alt="3" />

        <img src="5.jpg"  alt="5"/>

        <img src="1.jpg" alt="5" />

    </div>

       


    <div id="buttons">

        <span index="1" class="on"></span>

        <span index="2"></span>

        <span index="3"></span>

        <span index="4"></span>

        <span index="5"></span>

    </div>

    <a href="javascript:;" id="prev" class="arrow">&lt;</a>

    <a href="javascript:;" id="next" class="arrow">&gt;</a>

</div>














<div data-role="footer"  style="position: fixed; left: 0px; bottom: 0px; width: 100%; height: auto; z-index: 9999; font-size: 50px;">

    <div data-role="navbar">

<ul>

<li><a  href="Untitled-2.html" style="height: 20px; font-size: 20px; ">首页</a></li>

<li><a  href="#anylink" style="height:20px; font-size: 20px;">列表</a></li>

<li><a  href="Untitled-1.html" style="height:20px; font-size: 20px;">记录</a></li>

<li><a  href="gerenzhuye/Untitled-2.html" style="height:20px; font-size: 20px;">个人</a></li>

</ul>

</div>

  </div>

</div>


</body>


</html>


提问者:qq_登高休独倚月冷人心凉_0 2017-05-31 22:37

个回答

  • a14562
    2017-06-06 10:50:40

    样式中的width用百分比控制,js方法中获取宽度,这时获取的width就是不同设备下实时的宽度,通过这个宽度通过计算(1比1也好,还是其他比例也好)设置高度height,还有向左向右的偏移量list.style.left和list.style.right。