为了账号安全,请及时绑定邮箱和手机立即绑定

自动播放功能

自动播放怎么弄?setInterval,页面加载完成后加定时器规定两秒执行switchslider一次,但是每次执行都要i+1。做不出来。。

正在回答

4 回答

<!DOCTYPE html>

<html lang="zh-CN">

<head>

    <meta charset="UTF-8">

    <title>预览图幻灯片</title>

    <style type="text/css">

        h1,h2{

            margin:0;

            padding:0;

            font-weight:normal;

        }

        html,body{

            margin:0;

            padding:0;      

        }

        body{

            background-color:#fff;

            font-size:100%;

            color:#555;

            -webkit-font-smoothing:antialiased;

        }

        .slider{

            width:1280px;

            margin:0 auto;

            position:relative;

        }

        .slider .main{

            width:100%;

            height:400px;

            overflow:hidden;

            position:relative;

        }

        .slider .main .main-i{

            position:absolute;

            left:-50%;

            top:0;

            height:400px;

            opacity:0;

            transition:all 0.5s;

            -webkit-transition:all 0.5s;

            z-index:2;

        }

        .slider .main .main-i-right{

            left:50%;

        }

        #main_background,       

        .slider .main .main-i-active{

            left:0;

            opacity:1;

        }

        #main_background{

            z-index:1;

        }

        .slider .main .main-i .caption{

            position: absolute;

            right:58%;

            top:20%;

            text-align:right;

            z-index:2;

        }

        .slider .main .main-i .caption h2{

            font-size:2.6rem;

            line-height:3rem;

            color:#b5b5b5;

            margin-right:3rem;          

        }

        .slider .main .main-i .caption h1{

            font-size:4.6rem;

            color:#000;

            font-family:Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;

            margin-right:-5rem;

        }

        .slider .main .main-i .caption h1,

        .slider .main .main-i .caption h2{

            opacity:0;

            transition:all .8s 1s;

            -webkit-transition:all .8s .6s;

        }

        .slider .main .main-i-active .caption h1,

        .slider .main .main-i-active .caption h2{

            opacity:1;

            margin-right:0;

        }

        .slider .main .main-i img{

            display:block;

            position:relative;

            left:0;

            top:50%;

            z-index:1;

        }


        .slider .ctrl{

            position:absolute;

            left:0;

            bottom:-13px;

            height:13px;

            width:100%;

            text-align:center;

            z-index:9;

            font-size: 0;

        }

        .slider .ctrl .ctrl-i{

            display:inline-block;           

            position:relative;

            height:13px;

            width:150px;

            background-color:#666;

            margin-left:1px;

            box-shadow:0 1px 1px rgba(0,0,0,0.3);

            transition:all 0.3s;

            -webkit-transition:all 0.3s;

        }

        .slider .ctrl .ctrl-i img{

            display:block;

            width:100%;

            border:0;

            position:absolute;

            left:0;

            bottom:50px;

            transition:all 0.3s;

            -webkit-transition:all 0.3s;

            opacity:0;

            z-index:0;          

        }

        .slider .ctrl .ctrl-i:hover{

            background-color:#f0f0f0;

        }

        .slider .ctrl .ctrl-i:hover img{

            bottom:13px;

            opacity:1;

            -webkit-box-reflect:below 0px -webkit-gradient(

                linear,

                left top,

                left bottom,

                from(transparent),

                color-stop(50%,transparent),

                to(rgba(255,255,255,0.3))

            );

        }

        .slider .ctrl .ctrl-i-active:hover,

        .slider .ctrl .ctrl-i-active{

            background-color:#000;

        }

        .slider .ctrl .ctrl-i-active:hover img{

            opacity:0;

        }




    </style>

</head>

<body>

    <div class="slider">

        <!--  -->

        <div class="main" id="template_main">

            <div class="main-i {{css}}" id="main_{{index}}">

                <div class="caption">

                    <h2>{{h2}}</h2>

                    <h1>{{h1}}</h1>                 

                </div>

                <img src="./imgs/{{index}}.jpg" alt="" class="slider_pic" />

            </div>

        </div>


        <div class="ctrl" id="template_ctrl">

            <a class="ctrl-i" id="ctrl_{{index}}" href="javascript:switchSlider({{index}});">

                <img src="./imgs/{{index}}.jpg" alt="" />

            </a>

        </div>


    </div>


    <script type="text/javascript">

        var data=[

            {img:1,h1:'DUET',h2:'Creative'},

            {img:2,h1:'DEVIL',h2:'Friendly'},

            {img:3,h1:'COMPATRIOT',h2:'Tranquilent'},

            {img:4,h1:'HUSSLER',h2:'Insecure'},

            {img:5,h1:'REBEL',h2:'Loving'},

            {img:6,h1:'SEEKER',h2:'Passionate'},

            {img:7,h1:'FRIEND',h2:'Crazy'},

        ];

        var num=1;


        // 获取DOM节点

        var g=function(id){

            if(id.substr(0,1)=='.'){

                return document.getElementsByClassName(id.substr(1));

            }

            

            return document.getElementById(id);

            

        }


        // 添加幻灯片的操作及对应的按钮

        function addSliders(){

            var tpl_main=g('template_main').innerHTML

                            .replace('/^\s*/','')

                            .replace('/\s*$/','');

            var tpl_ctrl=g('template_ctrl').innerHTML

                            .replace('/^\s*/','')

                            .replace('/\s*$/','');

            

            // 定义最终输出的HTML的变量

            var out_main=[],

                out_ctrl=[];

            

            // 遍历数据

            for(i in data){

                var _html_main=tpl_main

                                .replace(/{{index}}/g,data[i].img)

                                .replace(/{{css}}/g,['','main-i-right'][i%2])

                                .replace(/{{h1}}/g,data[i].h1)

                                .replace(/{{h2}}/g,data[i].h2);

                var _html_ctrl=tpl_ctrl

                                .replace(/{{index}}/g,data[i].img);

                

                out_main.push(_html_main);

                out_ctrl.push(_html_ctrl);

            }


            g('template_main').innerHTML=out_main.join('');

            g('template_ctrl').innerHTML=out_ctrl.join('');


            // 加一背景图片

            g('template_main').innerHTML+=tpl_main

                                .replace(/{{h1}}/g,data[0].h1)

                                .replace(/{{h2}}/g,data[0].h2);

            g('main_{{index}}').id="main_background";


        }


        // 幻灯片切换

        function switchSlider(n){

            num=n;

            console.log(num);

            var main=g('main_'+n);

            var ctrl=g('ctrl_'+n);


            var clear_main=g('.main-i');

            var clear_ctrl=g('.ctrl-i');

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

                clear_ctrl[i].className=clear_ctrl[i].className.replace(' ctrl-i-active','');

                clear_main[i].className=clear_main[i].className.replace(' main-i-active','');

            }


            main.className+=' main-i-active';

            ctrl.className+=' ctrl-i-active';


            // 切换的时候把当前的图添加到背景图

            setTimeout(function(){

                g('main_background').innerHTML=main.innerHTML;

            },1000);

            

        }


        // 调整图片位置

        function movePics(){

            var slider_pics=g('.slider_pic');           

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

                slider_pics[i].style.marginTop=(-1*slider_pics[i].clientHeight/2)+'px';

            }

        }


        //自动播放

        function autoPlay(){                        

            setInterval(function(){

                console.log(num);

                switchSlider(num);

                num++;

                if(num>7){

                num=1;

                }

            },3000);

            

        }


        // 定义何时处理幻灯片输出

        window.onload=function(){

            addSliders();

            setTimeout(function(){

                movePics();

            },100);         

            switchSlider(1);

            autoPlay();

        }

    </script>

</body>

</html>



0 回复 有任何疑惑可以回复我~

var num=1 //全局变量,记录当前播放的幻灯片

var num=1; //定义为全局变量

switchSlide(s){
    num=s //在切换幻灯片方法中,手动点击,改变全局变量值,以便自动播放在点击处开始播放
}

//定义自动播放方法
function autoPlay(){
    setInterval(function(){
        switchSlider(num);
        num++;
        if(num>7){
            num=1;
             } },3000); //定时自动切换
}

//在DOM加载后,执行自动播放方法
window.onload=function(){
    autoPlay();

//测试,完美自动播放

0 回复 有任何疑惑可以回复我~
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Preview Slideshow</title>
<style>
*{
padding:0;
margin: 0;
}
body{
padding:50px 0;
background-color: #fff;
font-size:14px;
font-family:'Avenir Next';
color:#555;
-webkit-font-smoothing:antialiased;
/*字体抗锯齿*/
}
.slider .ctrl .ctrl-i,
.slider .main .main-i,
.slider .main,
.slider{
width: 100%;
height: 460px;
position:relative;
}
.slider .main{
overflow: hidden;

}

.slider .main .main-i img{
width: 100%;
position:absolute;
left: 0;
top:50%;
}
.slider .main .main-i .caption{
position: absolute;
right: 50%;
top: 30%;
z-index: 9;
}
.slider .main .main-i .caption h2{
font-size: 40px;
line-height: 50px;
color: #b5b5b5;
text-align: right;
}
.slider .main .main-i .caption h3{
text-align: right;
font-size: 70px;
line-height: 70px;
color: #000;
font-family: 'Open Sans Condensed';
}
.slider .main #main_background{
z-index: 1
}
.slider .ctrl{
width: 100%;
height: 13px;
line-height: 13px;
text-align: center;
position: absolute;
left: 0;
bottom: -13px;
}
.slider .ctrl .ctrl-i{
display: inline-block;
width: 150px;
height: 13px;
background-color: #666;
box-shadow: 0 1px 1px rgba(0,0,0,.3);
z-index: 10;
margin-left: 1px;
}
.slider .ctrl .ctrl-i img{
width: 100%;
position: absolute;
left: 0;
bottom: 50px;

}
/*制作鼠标经过ctrl的效果*/
.slider .ctrl .ctrl-i:hover{
background-color: #f0f0f0;
box-shadow: 0 1px 1px rgba(0,0,0,.3);
}
.slider .ctrl .ctrl-i:hover img{
bottom: 13px;
transition: all .3s ease-in-out;
-webkit-box-reflect:below 0 -webkit-gradient(
linear,
left top,
left bottom,
from(transparent),
color-stop(50%,transparent),
to( rgba(255,255,255,.3))
);
}

.slider .ctrl .ctrl-i-active,
.slider .ctrl .ctrl-i-active:hover{
background-color: #000;
}
.slider .ctrl .ctrl-i:hover img{
opacity: 1;
}
.slider .ctrl .ctrl-i img,
.slider .ctrl .ctrl-i-active img,
.slider .ctrl .ctrl-i-active:hover img{
opacity: 0;
}
.slider .main .main-i .caption{
margin-right: 13%;
}
.slider .main .main-i .caption h2{
margin-right: 45px;
}
.slider .main .main-i .caption h3{
margin-right: -45px;
}
#main_background,
.slider .main .main-i-active{
right: 0;
opacity: 1;
z-index: 2;
}
#main_background{
z-index: 1;
}
.slider .main .main-i-active .caption h2,
.slider .main .main-i-active .caption h3{
margin-right:0;
transition: all 1s 0.8s;
-moz-transition: all 1s 0.8s; /* Firefox 4 */
-webkit-transition: all 1s 0.8s; /* Safari 和 Chrome */
-o-transition: all 1s 0.8s; /* Opera */
}
.slider .main .main-i-active img{
position: absolute;
right: 50%;
top: 50%;
/*margin-top: 50%;*/
}

/*设置幻灯片的样式*/
.slider .main .main-i {
position: absolute;
right: 50%;
top: 0;
opacity: 0;
z-index: 2;
}
.slider .main .main-i-right{
right: -50%;
}
.slider .main .main-i-active{
transition: all 0.8s;
-moz-transition: all 0.8s; /* Firefox 4 */
-webkit-transition: all 0.8s; /* Safari 和 Chrome */
-o-transition: all 0.8s; /* Opera */
opacity: 1;
right: 0;
}

</style>
<script>
var data=[{img:'1',h1:'简单点',h2:'说话的方式简单点'},
{img:'2',h1:'递进的情绪请省略',h2:'你又不是个演员'},
{img:'3',h1:'别设计那些情节',h2:'没意见'},
{img:'4',h1:'我只想看看你怎么圆',h2:'你难过的太表面'},
{img:'5',h1:'像没天赋的演员',h2:'观众一眼能看见'},
{img:'6',h1:'该配合你演出的我',h2:'演视而不见'},
{img:'7',h1:'别逼一个最爱你的人',h2:'即兴表演'}
];
function g(id){
if(id.substr(0,1)=='.'){
return document.getElementsByClassName(id.substr(1));
}else{
return document.getElementById(id);
}
}
function AddSliders(){
var tpl_main = g('template_main').innerHTML;
var tpl_ctrl = g('template_ctrl').innerHTML;
var html_main = tpl_main.replace(/^\s*/,'').replace(/\s*$/,'');
var html_ctrl = tpl_ctrl.replace(/^\s*/,'').replace(/\s*$/,'');
// alert(html_main);
var out_main = [];
var out_ctrl = [];
for(i in data){
add_main = html_main.replace(/{{index}}/g,data[i].img)
.replace(/{{H2}}/g,data[i].h1)
.replace(/{{H3}}/g,data[i].h2)
.replace(/{{css}}/g,['','main-i-right'][i%2]);
add_ctrl = html_ctrl.replace(/{{index}}/g,data[i].img);
//在数组的末尾添加add_main并用逗号分隔
out_main.push(add_main);
out_ctrl.push(add_ctrl);
}
//这里要使用join('')来替换掉自动会生成的','
g('template_main').innerHTML = out_main.join('');
g('template_ctrl').innerHTML = out_ctrl.join('');
g('template_main').innerHTML += html_main.replace(/{{index}}/g,'{{index}}')
.replace(/{{H2}}/g,data[i].h1)
.replace(/{{H3}}/g,data[i].h2);
g('main_{{index}}').id = 'main_background';
}

function switchSlide(s){
var show_main = g('main_'+s);
var show_ctrl = g('ctrl_'+s);
var clear_main = g('.main-i');
         var clear_ctrl = g('.ctrl-i');
for (var i = 0; i < g('.ctrl-i').length; i++) {
clear_main[i].className = clear_main[i].className.replace(' main-i-active','');
clear_ctrl[i].className = clear_ctrl[i].className.replace(' ctrl-i-active','');
}
show_main.className += ' main-i-active';
         show_ctrl.className += ' ctrl-i-active';
setTimeout(function(){
g('main_background').innerHTML = show_main.innerHTML;
},1000)
n=parseInt(show_main.id.substr(5));
}
function setImgheight(){
var pictures = g('.picture');
for (var i = 0; i < pictures.length; i++) {
pictures[i].style.marginTop = (-1*pictures[i].clientHeight/2)+'px';
}
}
var n;
function autoplay(){
switchSlide(n);
n++;
if(n>7){
n=1;
}
}
window.onload = function(){
AddSliders();
setTimeout(function(){
setImgheight();
},100)
//页面加载时执行第一张图的点击效果,5秒后调用自动播放函数,再过两秒切换到第一张,再过两秒切第二张
switchSlide(1);
setInterval(function(){
autoplay();
},4000)
}

</script>
</head>
<body>
<div>

<div id="template_main">
<div class="main-i {{css}}" id="main_{{index}}">
<div>
<h2>{{H2}}</h2>
<h3>{{H3}}</h3>
</div>
<img src="imgs/{{index}}.jpg" alt="">
</div>
</div>

<div id="template_ctrl">
<a id="ctrl_{{index}}" href="javascript:switchSlide({{index}});">
<img src="imgs/{{index}}.jpg" alt="">
</a>
</div>

</div>
</body>
</html>

这样放代码好像才能用

0 回复 有任何疑惑可以回复我~
#1

请叫我橙子蛋 提问者

好像还是不行
2017-03-16 回复 有任何疑惑可以回复我~
走了一个循环遍历改变I值然后延时触发点击事件的弯路,发现循环它不会等你动画执行完再加一。后来设全局变量解决的,本人菜鸡,代码肯定还能优化。

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Preview Slideshow</title>

<style>

*{

padding:0;

margin: 0;

}

body{

padding:50px 0;

background-color: #fff;

font-size:14px;

font-family:'Avenir Next';

color:#555;

-webkit-font-smoothing:antialiased;

/*字体抗锯齿*/

}

.slider .ctrl .ctrl-i,

.slider .main .main-i,

.slider .main,

.slider{

width: 100%;

height: 460px;

position:relative;

}

.slider .main{

overflow: hidden;


}


.slider .main .main-i img{

width: 100%;

position:absolute;

left: 0;

top:50%;

}

.slider .main .main-i .caption{

position: absolute;

right: 50%;

top: 30%;

z-index: 9;

}

.slider .main .main-i .caption h2{

font-size: 40px;

line-height: 50px;

color: #b5b5b5;

text-align: right;

}

.slider .main .main-i .caption h3{

text-align: right;

font-size: 70px;

line-height: 70px;

color: #000;

font-family: 'Open Sans Condensed';

}

.slider .main #main_background{

z-index: 1

}

.slider .ctrl{

width: 100%;

height: 13px;

line-height: 13px;

text-align: center;

position: absolute;

left: 0;

bottom: -13px;

}

.slider .ctrl .ctrl-i{

display: inline-block;

width: 150px;

height: 13px;

background-color: #666;

box-shadow: 0 1px 1px rgba(0,0,0,.3);

z-index: 10;

margin-left: 1px;

}

.slider .ctrl .ctrl-i img{

width: 100%;

position: absolute;

left: 0;

bottom: 50px;


}

/*制作鼠标经过ctrl的效果*/

.slider .ctrl .ctrl-i:hover{

background-color: #f0f0f0;

box-shadow: 0 1px 1px rgba(0,0,0,.3);

}

.slider .ctrl .ctrl-i:hover img{

bottom: 13px;

transition: all .3s ease-in-out;

-webkit-box-reflect:below 0 -webkit-gradient(

linear,

left top,

left bottom,

from(transparent),

color-stop(50%,transparent),

to( rgba(255,255,255,.3))

);

}


.slider .ctrl .ctrl-i-active,

.slider .ctrl .ctrl-i-active:hover{

background-color: #000;

}

.slider .ctrl .ctrl-i:hover img{

opacity: 1;

}

.slider .ctrl .ctrl-i img,

.slider .ctrl .ctrl-i-active img,

.slider .ctrl .ctrl-i-active:hover img{

opacity: 0;

}

.slider .main .main-i .caption{

margin-right: 13%;

}

.slider .main .main-i .caption h2{

margin-right: 45px;

}

.slider .main .main-i .caption h3{

margin-right: -45px;

}

#main_background,

.slider .main .main-i-active{

right: 0;

opacity: 1;

z-index: 2;

}

#main_background{

z-index: 1;

}

.slider .main .main-i-active .caption h2,

.slider .main .main-i-active .caption h3{

margin-right:0;

transition: all 1s 0.8s;

-moz-transition: all 1s 0.8s; /* Firefox 4 */

-webkit-transition: all 1s 0.8s; /* Safari 和 Chrome */

-o-transition: all 1s 0.8s; /* Opera */

}

.slider .main .main-i-active img{

position: absolute;

right: 50%;

top: 50%;

/*margin-top: 50%;*/

}


/*设置幻灯片的样式*/

.slider .main .main-i {

position: absolute;

right: 50%;

top: 0;

opacity: 0;

z-index: 2;

}

.slider .main .main-i-right{

right: -50%;

}

.slider .main .main-i-active{

transition: all 0.8s;

-moz-transition: all 0.8s; /* Firefox 4 */

-webkit-transition: all 0.8s; /* Safari 和 Chrome */

-o-transition: all 0.8s; /* Opera */

opacity: 1;

right: 0;

}


</style>

<script>

var data=[{img:'1',h1:'简单点',h2:'说话的方式简单点'},

{img:'2',h1:'递进的情绪请省略',h2:'你又不是个演员'},

{img:'3',h1:'别设计那些情节',h2:'没意见'},

{img:'4',h1:'我只想看看你怎么圆',h2:'你难过的太表面'},

{img:'5',h1:'像没天赋的演员',h2:'观众一眼能看见'},

{img:'6',h1:'该配合你演出的我',h2:'演视而不见'},

{img:'7',h1:'别逼一个最爱你的人',h2:'即兴表演'}

];

function g(id){

if(id.substr(0,1)=='.'){

return document.getElementsByClassName(id.substr(1));

}else{

return document.getElementById(id);

}

}

function AddSliders(){

var tpl_main = g('template_main').innerHTML;

var tpl_ctrl = g('template_ctrl').innerHTML;

var html_main = tpl_main.replace(/^\s*/,'').replace(/\s*$/,'');

var html_ctrl = tpl_ctrl.replace(/^\s*/,'').replace(/\s*$/,'');

// alert(html_main);

var out_main = [];

var out_ctrl = [];

for(i in data){

add_main = html_main.replace(/{{index}}/g,data[i].img)

.replace(/{{H2}}/g,data[i].h1)

.replace(/{{H3}}/g,data[i].h2)

.replace(/{{css}}/g,['','main-i-right'][i%2]);

add_ctrl = html_ctrl.replace(/{{index}}/g,data[i].img);

//在数组的末尾添加add_main并用逗号分隔

out_main.push(add_main);

out_ctrl.push(add_ctrl);

}

//这里要使用join('')来替换掉自动会生成的','

g('template_main').innerHTML = out_main.join('');

g('template_ctrl').innerHTML = out_ctrl.join('');

g('template_main').innerHTML += html_main.replace(/{{index}}/g,'{{index}}')

.replace(/{{H2}}/g,data[i].h1)

.replace(/{{H3}}/g,data[i].h2);

g('main_{{index}}').id = 'main_background';

}


function switchSlide(s){

var show_main = g('main_'+s);

var show_ctrl = g('ctrl_'+s);

var clear_main = g('.main-i');

         var clear_ctrl = g('.ctrl-i');

for (var i = 0; i < g('.ctrl-i').length; i++) {

clear_main[i].className = clear_main[i].className.replace(' main-i-active','');

clear_ctrl[i].className = clear_ctrl[i].className.replace(' ctrl-i-active','');

}

show_main.className += ' main-i-active';

         show_ctrl.className += ' ctrl-i-active';

setTimeout(function(){

g('main_background').innerHTML = show_main.innerHTML;

},1000)

n=parseInt(show_main.id.substr(5));

}

function setImgheight(){

var pictures = g('.picture');

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

pictures[i].style.marginTop = (-1*pictures[i].clientHeight/2)+'px';

}

}

var n;

function autoplay(){

switchSlide(n);

n++;

if(n>7){

n=1;

}

}

window.onload = function(){

AddSliders();

setTimeout(function(){

setImgheight();

},100)

//页面加载时执行第一张图的点击效果,5秒后调用自动播放函数,再过两秒切换到第一张,再过两秒切第二张

switchSlide(1);

setInterval(function(){

autoplay();

},4000)

}


</script>

</head>

<body>

<div>


<div id="template_main">

<div class="main-i {{css}}" id="main_{{index}}">

<div>

<h2>{{H2}}</h2>

<h3>{{H3}}</h3>

</div>

<img src="imgs/{{index}}.jpg" alt="">

</div>

</div>


<div id="template_ctrl">

<a id="ctrl_{{index}}" href="javascript:switchSlide({{index}});">

<img src="imgs/{{index}}.jpg" alt="">

</a>

</div>


</div>

</body>

</html>


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

自动播放功能

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信