轮播图dw可以正常显示,浏览器却不行

qq_39087005 2018-05-12 05:28:59
在网上找了一段代码,dw可以正常播放,浏览器图却不动
求大神帮忙看看!!谢谢大家
代码如下:

<body>

<div id="container">
<div id="list" style="left: -600px;">
<img src="../images/轮播3.png" alt="1"/>
<img src="../images/轮播1.png" alt="1"/>
<img src="../images/轮播2.png" alt="2"/>
<img src="../images/轮播3.png" alt="3"/>
<img src="../images/轮播1.png" alt="4"/>

</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>

</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>

</body>


<style type="text/css">
* {
margin: 0;
padding: 0;
text-decoration: none;
}
body {
padding: 20px;
}
#container {
position: relative;
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
}
#list {
position: absolute;
z-index: 1;
width: 4200px;
height: 400px;
}
#list img {
float: left;
width: 600px;
height: 400px;
}
#buttons {
position: absolute;
left: 250px;
bottom: 20px;
z-index: 2;
height: 10px;
width: 100px;
}
#buttons span {
float: left;
margin-right: 5px;
width: 10px;
height: 10px;
border: 1px solid #fff;
border-radius: 50%;
background: #333;
cursor: pointer;
}
#buttons .on {
background: orangered;
}
.arrow {
position: absolute;
top: 180px;
z-index: 2;
display: none;
width: 40px;
height: 40px;
font-size: 36px;
font-weight: bold;
line-height: 39px;
text-align: center;
color: #fff;
background-color: RGBA(0, 0, 0, .3);
cursor: pointer;
}
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
}
#container:hover .arrow {
display: block;
}
#prev {
left: 20px;
}
#next {
right: 20px;
}
</style>


<script type="text/javascript">
/* 知识点: */
/* this用法 */
/* DOM事件 */
/* 定时器 */
window.onload = function () {
var container = document.getElementsById('container');
var list = document.getElementsById('list');
var buttons = document.getElementsById('buttons').getElementsByTagName('span');
var prev = document.getElementsById('prev');
var next = document.getElementsById('next');
var index = 1;
var timer;
function animate(offset) {
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()取整转化为数字。
var newLeft = parseInt(list.style.left) + offset;
list.style.left = newLeft + 'px';
//无限滚动判断
if(newLeft < -1800){
list.style.left = -600 + 'px';
}
else if(newLeft > 0){
list.style.left = -2400 + 'px';
}
}
function play() {
//重复执行的定时器
timer = setInterval(function () {
next.onclick();
}, 2000)
}
function stop() {
clearInterval(timer);
}
function buttonsShow() {
//将之前的小圆点的样式清除
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == "on") {
buttons[i].className = "";
}
}
//数组从0开始,故index需要-1
buttons[index - 1].className = "on";
}
prev.onclick = function () {
index -= 1;
if (index < 1) {
index = 5
}
buttonsShow();
animate(600);
};
next.onclick = function () {
//由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
index += 1;
if (index > 5) {
index = 1
}
animate(-600);
buttonsShow();
};
for (var i = 0; i < buttons.length; i++) {
(function (i) {
buttons[i].onclick = function () {
/* 这里获得鼠标移动到小圆点的位置,用this把index绑定到对象buttons[i]上,去谷歌this的用法 */
/* 由于这里的index是自定义属性,需要用到getAttribute()这个DOM2级方法,去获取自定义index的属性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (index - clickIndex); //这个index是当前图片停留时的index
animate(offset);
index = clickIndex; //存放鼠标点击后的位置,用于小圆点的正常显示
buttonsShow();
}
})(i)
}
container.onmouseover = stop;
container.onmouseout = play;
play();
}
</script>
...全文
2567 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39087005 2018-05-14
  • 打赏
  • 举报
回复
引用 10 楼 jslang 的回复:
[quote=引用 9 楼 qq_39087005 的回复:] [quote=引用 8 楼 jslang 的回复:] [quote=引用 7 楼 qq_39087005 的回复:] [quote=引用 6 楼 jslang 的回复:] 当然可以啊 你可能是文件编码的问题。文件要保存为utf-8编码
我是完全复制粘贴您给出的代码的…… 而且也清除了浏览器缓存 可是浏览器就是没有反应…… 轮播图上用于切换图片的“小圆点”也点不动…… [/quote] 文件保存为utf-8编码 或者代码中<meta charset="UTF-8" />改成<meta charset="GB2312" /> [/quote] 改了之后还是不行……… (真的非常感谢您)[/quote] 你这还真是奇怪。难道是浏览器禁用了js?[/quote] 其他地方用了一点js,浏览器可以显示…… 不知道问题出在哪里
天际的海浪 2018-05-14
  • 打赏
  • 举报
回复
引用 9 楼 qq_39087005 的回复:
[quote=引用 8 楼 jslang 的回复:] [quote=引用 7 楼 qq_39087005 的回复:] [quote=引用 6 楼 jslang 的回复:] 当然可以啊 你可能是文件编码的问题。文件要保存为utf-8编码
我是完全复制粘贴您给出的代码的…… 而且也清除了浏览器缓存 可是浏览器就是没有反应…… 轮播图上用于切换图片的“小圆点”也点不动…… [/quote] 文件保存为utf-8编码 或者代码中<meta charset="UTF-8" />改成<meta charset="GB2312" /> [/quote] 改了之后还是不行……… (真的非常感谢您)[/quote] 你这还真是奇怪。难道是浏览器禁用了js?
qq_39087005 2018-05-13
  • 打赏
  • 举报
回复
引用 1 楼 jslang 的回复:
代码错误很多,主要有 getElementById 你都多了个 s 5张图片,下面的span却只有3个 animate()函数中无限滚动判断的范围也不对
我改了一下。 可还是老问题:在dw能正常运行,google里不播放 求大神帮助
qq_39087005 2018-05-13
  • 打赏
  • 举报
回复
我改了一下。 可还是老问题:在dw能正常运行,google里不播放
qq_39087005 2018-05-13
  • 打赏
  • 举报
回复
引用 8 楼 jslang 的回复:
[quote=引用 7 楼 qq_39087005 的回复:] [quote=引用 6 楼 jslang 的回复:] 当然可以啊 你可能是文件编码的问题。文件要保存为utf-8编码
我是完全复制粘贴您给出的代码的…… 而且也清除了浏览器缓存 可是浏览器就是没有反应…… 轮播图上用于切换图片的“小圆点”也点不动…… [/quote] 文件保存为utf-8编码 或者代码中<meta charset="UTF-8" />改成<meta charset="GB2312" /> [/quote] 改了之后还是不行……… (真的非常感谢您
天际的海浪 2018-05-13
  • 打赏
  • 举报
回复
引用 7 楼 qq_39087005 的回复:
[quote=引用 6 楼 jslang 的回复:] 当然可以啊 你可能是文件编码的问题。文件要保存为utf-8编码
我是完全复制粘贴您给出的代码的…… 而且也清除了浏览器缓存 可是浏览器就是没有反应…… 轮播图上用于切换图片的“小圆点”也点不动…… [/quote] 文件保存为utf-8编码 或者代码中<meta charset="UTF-8" />改成<meta charset="GB2312" />
qq_39087005 2018-05-13
  • 打赏
  • 举报
回复
引用 6 楼 jslang 的回复:
当然可以啊
你可能是文件编码的问题。文件要保存为utf-8编码

我是完全复制粘贴您给出的代码的……
而且也清除了浏览器缓存
可是浏览器就是没有反应……
轮播图上用于切换图片的“小圆点”也点不动……
天际的海浪 2018-05-13
  • 打赏
  • 举报
回复
当然可以啊 你可能是文件编码的问题。文件要保存为utf-8编码
qq_39087005 2018-05-13
  • 打赏
  • 举报
回复
引用 4 楼 jslang 的回复:
[quote=引用 3 楼 qq_39087005 的回复:] [quote=引用 1 楼 jslang 的回复:] 代码错误很多,主要有 getElementById 你都多了个 s 5张图片,下面的span却只有3个 animate()函数中无限滚动判断的范围也不对
我改了一下。 可还是老问题:在dw能正常运行,google里不播放 求大神帮助[/quote]

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<title> 页面名称 </title>

<style type="text/css">
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
        }
        body {
            padding: 20px;
        }
        #container {
            position: relative;
            width: 600px;
            height: 400px;
            border: 3px solid #333;
            overflow: hidden;
        }
        #list {
            position: absolute;
            z-index: 1;
            width: 4200px;
            height: 400px;
        }
        #list img {
            float: left;
            width: 600px;
            height: 400px;
        }
        #buttons {
            position: absolute;
            left: 250px;
            bottom: 20px;
            z-index: 2;
            height: 10px;
            width: 100px;
        }
        #buttons span {
            float: left;
            margin-right: 5px;
            width: 10px;
            height: 10px;
            border: 1px solid #fff;
            border-radius: 50%;
            background: #333;
            cursor: pointer;
        }
        #buttons .on {
            background: orangered;
        }
        .arrow {
            position: absolute;
            top: 180px;
            z-index: 2;
            display: none;
            width: 40px;
            height: 40px;
            font-size: 36px;
            font-weight: bold;
            line-height: 39px;
            text-align: center;
            color: #fff;
            background-color: RGBA(0, 0, 0, .3);
            cursor: pointer;
        }
        .arrow:hover {
            background-color: RGBA(0, 0, 0, .7);
        }
        #container:hover .arrow {
            display: block;
        }
        #prev {
            left: 20px;
        }
        #next {
            right: 20px;
        }
    </style>

<script type="text/javascript">
        /* 知识点:        */
        /*       this用法 */
        /*       DOM事件 */
        /*       定时器 */
        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 = 0;
            var len = list.getElementsByTagName("img").length;
            var width = parseInt((container.currentStyle||getComputedStyle(container,null)).width
            , 10);
            var timer;
            function animate() {
                list.style.left = (-width*index) + 'px';
            }
            function play() {
                //重复执行的定时器
                timer = setInterval(function () {
                    next.onclick();
                }, 2000)
            }
            function stop() {
                clearInterval(timer);
            }
            function buttonsShow() {
                //将之前的小圆点的样式清除
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons[i].className == "on") {
                        buttons[i].className = "";
                    }
                }
                //数组从0开始,故index需要-1
                buttons[index].className = "on";
            }
            prev.onclick = function () {
                index -= 1;
                if (index < 0) {
                    index = len-1
                }
                animate();
                buttonsShow();
            };
            next.onclick = function () {
                //由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
                index += 1;
                if (index >= len) {
                    index = 0
                }
                animate();
                buttonsShow();
            };
            for (var i = 0; i < buttons.length; i++) {
                (function (i) {
	                buttons[i].index = i;
                    buttons[i].onclick = function () {
                        index = this.index; //存放鼠标点击后的位置,用于小圆点的正常显示
                        animate();
                        buttonsShow();
                    }
                })(i)
            }
            container.onmouseover = stop;
            container.onmouseout = play;
            play();
        }
    </script>

</head>
<body>

<div id="container">
    <div id="list" style="left: 0px;">
        <img src="../images/轮播3.png" alt="0"/>
        <img src="../images/轮播1.png" alt="1"/>
        <img src="../images/轮播2.png" alt="2"/>
        <img src="../images/轮播3.png" alt="3"/>
        <img src="../images/轮播1.png" alt="4"/>

    </div>
    <div id="buttons">
        <span class="on"></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>

    </div>
    <a href="javascript:;" id="prev" class="arrow"><</a>
    <a href="javascript:;" id="next" class="arrow">></a>
</div>

</body>
</html>
[/quote] 这个代码在您的电脑浏览器里可以滑动吗? 我的浏览器还是没反应啊啊啊啊啊,虽然dw可以正常运行……
天际的海浪 2018-05-13
  • 打赏
  • 举报
回复
引用 3 楼 qq_39087005 的回复:
[quote=引用 1 楼 jslang 的回复:] 代码错误很多,主要有 getElementById 你都多了个 s 5张图片,下面的span却只有3个 animate()函数中无限滚动判断的范围也不对
我改了一下。 可还是老问题:在dw能正常运行,google里不播放 求大神帮助[/quote]

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<title> 页面名称 </title>

<style type="text/css">
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
        }
        body {
            padding: 20px;
        }
        #container {
            position: relative;
            width: 600px;
            height: 400px;
            border: 3px solid #333;
            overflow: hidden;
        }
        #list {
            position: absolute;
            z-index: 1;
            width: 4200px;
            height: 400px;
        }
        #list img {
            float: left;
            width: 600px;
            height: 400px;
        }
        #buttons {
            position: absolute;
            left: 250px;
            bottom: 20px;
            z-index: 2;
            height: 10px;
            width: 100px;
        }
        #buttons span {
            float: left;
            margin-right: 5px;
            width: 10px;
            height: 10px;
            border: 1px solid #fff;
            border-radius: 50%;
            background: #333;
            cursor: pointer;
        }
        #buttons .on {
            background: orangered;
        }
        .arrow {
            position: absolute;
            top: 180px;
            z-index: 2;
            display: none;
            width: 40px;
            height: 40px;
            font-size: 36px;
            font-weight: bold;
            line-height: 39px;
            text-align: center;
            color: #fff;
            background-color: RGBA(0, 0, 0, .3);
            cursor: pointer;
        }
        .arrow:hover {
            background-color: RGBA(0, 0, 0, .7);
        }
        #container:hover .arrow {
            display: block;
        }
        #prev {
            left: 20px;
        }
        #next {
            right: 20px;
        }
    </style>

<script type="text/javascript">
        /* 知识点:        */
        /*       this用法 */
        /*       DOM事件 */
        /*       定时器 */
        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 = 0;
            var len = list.getElementsByTagName("img").length;
            var width = parseInt((container.currentStyle||getComputedStyle(container,null)).width
            , 10);
            var timer;
            function animate() {
                list.style.left = (-width*index) + 'px';
            }
            function play() {
                //重复执行的定时器
                timer = setInterval(function () {
                    next.onclick();
                }, 2000)
            }
            function stop() {
                clearInterval(timer);
            }
            function buttonsShow() {
                //将之前的小圆点的样式清除
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons[i].className == "on") {
                        buttons[i].className = "";
                    }
                }
                //数组从0开始,故index需要-1
                buttons[index].className = "on";
            }
            prev.onclick = function () {
                index -= 1;
                if (index < 0) {
                    index = len-1
                }
                animate();
                buttonsShow();
            };
            next.onclick = function () {
                //由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
                index += 1;
                if (index >= len) {
                    index = 0
                }
                animate();
                buttonsShow();
            };
            for (var i = 0; i < buttons.length; i++) {
                (function (i) {
	                buttons[i].index = i;
                    buttons[i].onclick = function () {
                        index = this.index; //存放鼠标点击后的位置,用于小圆点的正常显示
                        animate();
                        buttonsShow();
                    }
                })(i)
            }
            container.onmouseover = stop;
            container.onmouseout = play;
            play();
        }
    </script>

</head>
<body>

<div id="container">
    <div id="list" style="left: 0px;">
        <img src="../images/轮播3.png" alt="0"/>
        <img src="../images/轮播1.png" alt="1"/>
        <img src="../images/轮播2.png" alt="2"/>
        <img src="../images/轮播3.png" alt="3"/>
        <img src="../images/轮播1.png" alt="4"/>

    </div>
    <div id="buttons">
        <span class="on"></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>

    </div>
    <a href="javascript:;" id="prev" class="arrow"><</a>
    <a href="javascript:;" id="next" class="arrow">></a>
</div>

</body>
</html>
天际的海浪 2018-05-12
  • 打赏
  • 举报
回复
代码错误很多,主要有 getElementById 你都多了个 s 5张图片,下面的span却只有3个 animate()函数中无限滚动判断的范围也不对

61,112

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧