您的位置 首页 > 数码极客

css3如何通过js触发播放

之前为大家带来过轮播图的写法 ,因为里面用到了运动框架 所以用起来还是很多不懂的,今天优化了一下写法,并且增加了左右切换

先看下html结构

<div id="container">

<div id="list" style="left:0; ">

<ul>

<li><img src="img2; alt="3"/></li>

<li><img src="img2; alt="2"/></li>

<li><img src="img2; alt="4"/></li>

<li><img src="img2; alt="5"/></li>

<li><img src="img2; alt="5"/></li>

</ul>

</div><!-- 存放图片的div -->

<div id="buttons">

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

<span></span>

<span></span>

<span></span>

<span></span>

</div><!-- 小圆点控制 -->

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

<a href="javascript:;" id="next" class="arrow">&gt;</a><!-- 左右切换 -->

</div>

是干什么用的上面都有注释

2

CSS样式

<style type="text/css">

*{ margin: 0; padding: 0; text-decoration: none;}

body { padding: 20px;}

#container { width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; position: relative;}/**大盒子**/

#list { height: 400px; position: absolute; z-index: 1;transition: all 1s;}/**过渡属性,没考虑兼容**/

#list ul li{width: 600px;list-style: none;float: left;}

#list img { min-width: 100%;}

#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: 180px; 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>

完成后 的效果图

3

下面开始编写js脚本

老规矩 先把想要的东西拿到手

var container = document.getElementById('container');//获取大盒子

var list = document.getElementById('list');//获取第二层盒子

var ul=li('ul')[0];//获取ul

var images=ul.getElementsByTagName('img');//获取img

var buttons = document.getElementById('buttons').getElementsByTagName('span');//获取好控制的span

var prev = document.getElementById('prev');//上一张

var next = document.getElementById('next');//下一张

var img=600;

ul.=images.length*img+'px';//设置ul的宽度

先来左右切换的效果

next.onclick=function(){

li parseIn)-600+'px';

}

(){

li parseIn)+600+'px';

}

//代码重复 而且会有留白,可以封装成一个函数

改进下

function animate(pare){

li parseIn)+pare+'px';

}

next.onclick=function(){

if(li= -2400 + 'px'){

li0 + 'px';

}

animate(-600);

}

(){

if(li= 0 + 'px'){

li -2400 + 'px';

}

animate(600);

}

下面开始做小圆点变色和控制图片

依旧是写在一个函数里

也可以切换了

move();

function move(){

for(var i=0;i<bu;i++){

buttons[i].index=i;

buttons[i].onclick= function () {

for(var i=0;i<bu;i++){

buttons[i].className=""

}

buttons[].className="on";

li-600*+"px";//控制切换

}

}

}

自动播放

play();

function play(){

var timer=setInterval(function(){

next.onclick();

},2000)

}

剩下就是左右切换 下面小圆点跟随了

需要给个变量

var index =1;

function setOut(){

for(var i=0;i<bu;i++){

buttons[i].className=''";

break;

}

}

buttons[index-1].className="on"

}

分别在作于里面调用下这个函数

下面是全部代码

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

<style type="text/css">

*{ margin: 0; padding: 0; text-decoration: none;}

body { padding: 20px;}

#container { width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; position: relative;}

#list { height: 400px; position: absolute; z-index: 1;transition: all 1s;}

#list ul li{width: 600px;list-style: none;float: left;}

#list img { min-width: 100%;}

#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: 180px; 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 id="container">

<div id="list" style="left:0; ">

<ul>

<li><img src="img2; alt="3"/></li>

<li><img src="img2; alt="2"/></li>

<li><img src="img2; alt="4"/></li>

<li><img src="img2; alt="5"/></li>

<li><img src="img2; alt="5"/></li>

</ul>

</div><!-- 存放图片的div -->

<div id="buttons">

<span></span>

<span></span>

<span></span>

<span></span>

<span></span>

</div>

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

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

</div>

<script>

window.onload=function(){

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

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

var ul = li('ul')[0];

var images = ul.getElementsByTagName('img');

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

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

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

var img = 600;

var index = 1

ul. = images.length * img + 'px';

function setOut() {

for (var i = 0; i < bu; i++) {

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

buttons[i].className = ''

break;

}

}

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

}

function animate(pare){

li parseIn)+pare+'px';

}

next.onclick=function(){

if(li= -2400 + 'px'){

li0 + 'px';

}

if(index==bu){

index=1;

}else{

index++;

}

animate(-600);

setOut();

}

(){

if(li= 0 + 'px'){

li -2400 + 'px';

}

if(index==1){

index=bu;

}else{

index--;

}

animate(600);

setOut();

}

move();

function move(){

for(var i=0;i<bu;i++){

buttons[i].index=i;

buttons[i].onclick= function () {

for(var i=0;i<bu;i++){

buttons[i].className=""

}

buttons[].className="on";

li-600*+"px";

}

}

}

paly();

function paly(){

var timer=setInterval(function () {

next.onclick();

},2000)

}

}

</script>

</body>

</html>

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“css3如何通过js触发播放”边界阅读