課程
/前端開發(fā)
/JavaScript
/焦點圖輪播特效
幫助萌新學(xué)習(xí)
2017-12-25
源自:焦點圖輪播特效 2-1
正在回答
<!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 {? ? ? ? ? ?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></head><body><!--父容器container存放所有內(nèi)容,? ?子容器list存放圖片。? ?子容器buttons存放按鈕小圓點--><div id="container">? ? ? <div id="list" style="left: -600px">? ? ? ? ? <img src="img/05.jpg" alt="1">? ? ? ? ? <img src="img/01.jpg" alt="1">? ? ? ? ? <img src="img/02.jpg" alt="2">? ? ? ? ? <img src="img/03.jpg" alt="3">? ? ? ? ? <img src="img/04.jpg" alt="4">? ? ? ? ? <img src="img/05.jpg" alt="5">? ? ? ? ? <img src="img/01.jpg" alt="5">? ? ? </div>? ? ? <div id="buttons">? ? ? ? ? <span index="1"></span>? ? ? ? ? <span index="2"></span>? ? ? ? ? <span index="3"></span>? ? ? ? ? <span index="4"></span>? ? ? ? ? <span index="5"></span>? ? ? </div>? ? ? <a href="javascript:;" id="prev"><</a>? ? ? <a href="javascript:;" id="next">></a>? </div><script>window.onload=function (){? ? ? ?var ?container =document.getElementById('container');var list=document.getElementById('list');var buttons=document.getElementById('buttons').getElementsByTagName('span');//獲取5個按鈕var prev=document.getElementById('prev');//上一個var next=document.getElementById('next');//下一個var index=1;var timer;/*點亮小圓點*/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 animate(offset){//封裝按鈕左右點擊,傳參offsetvar newLeft=parseInt(list.style.left)+offset;var time=300;//位移總時間var interval=10;//位移間隔var speed=offset/(time/interval);//每次位移量function go(){? ? ? ? ? ? ? ?if((speed<0 && parseInt(list.style.left)>newLeft) || (speed >0&&parseInt(list.style.left)<newLeft)){? ? ? ? ? ? ? ? ? ?list.style.left=parseInt(list.style.left)+speed+'px';/*定時器*/setTimeout(go,interval);}? ? ? ? ? ? ? ?else{? ? ? ? ? ? ? ? ? ?list.style.left=newLeft+'px';/* 判斷是否滾動到附屬圖上,滾動到附屬圖上歸位*/if(newLeft> -600){? ? ? ? ? ? ? ? ? ? ? ?list.style.left = -3000 + 'px';}? ? ? ? ? ? ? ? ? ?if(newLeft< -3000){? ? ? ? ? ? ? ? ? ? ? ?list.style.left = -600 + 'px';}? ? ? ? ? ? ? ?}? ? ? ? ? ?}? ? ? ? ? ? ? ?go();}? ? ? ?function play(){? ? ? ? ? ?timer=setInterval(function (){? ? ? ? ? ? ? ?next.onclick();},3000);}? ? ? ?function stop(){? ? ? ? ? ?clearInterval(timer);}? ? ? ?next.onclick=function (){? ? ? ? ? ?if(index==5){? ? ? ? ? ? ? ?index=1;}? ? ? ? ? ?else{? ? ? ? ? ? ? ?index +=1;}? ? ? ? ? ? ? ? ? ?showButton();animate(-600);//點擊右箭頭-600}? ? ? ?prev.onclick=function (){? ? ? ? ? ?if(index==1){? ? ? ? ? ? ? ?index=5;}? ? ? ? ? ?else{? ? ? ? ? ? ? ?index -= 1;}? ? ? ? ? ?showButton();animate(-600);//點擊左箭頭600}? ? ? ?for(var i=0;i<buttons.length;i++){? ? ? ? ? ?//原始值buttons[i].onclick=function (){? ? ? ? ? ? ? ?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;}</script></body></html>
舉報
通過本教程學(xué)習(xí)您將能掌握非常實用的焦點圖輪播特效的制作過程
1 回答誰有完整的代碼粘貼出來共享一下
1 回答輪播圖片切換不完整
1 回答求全部的代碼
1 回答關(guān)于源代碼的疑問
2 回答關(guān)于源代碼的問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-12-25
<!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 {
? ? ? ? ? ?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>
</head>
<body>
<!--父容器container存放所有內(nèi)容,
? ?子容器list存放圖片。
? ?子容器buttons存放按鈕小圓點
-->
<div id="container">
? ? ? <div id="list" style="left: -600px">
? ? ? ? ? <img src="img/05.jpg" alt="1">
? ? ? ? ? <img src="img/01.jpg" alt="1">
? ? ? ? ? <img src="img/02.jpg" alt="2">
? ? ? ? ? <img src="img/03.jpg" alt="3">
? ? ? ? ? <img src="img/04.jpg" alt="4">
? ? ? ? ? <img src="img/05.jpg" alt="5">
? ? ? ? ? <img src="img/01.jpg" alt="5">
? ? ? </div>
? ? ? <div id="buttons">
? ? ? ? ? <span index="1"></span>
? ? ? ? ? <span index="2"></span>
? ? ? ? ? <span index="3"></span>
? ? ? ? ? <span index="4"></span>
? ? ? ? ? <span index="5"></span>
? ? ? </div>
? ? ? <a href="javascript:;" id="prev"><</a>
? ? ? <a href="javascript:;" id="next">></a>
? </div>
<script>
window.onload=function (){
? ? ? ?var ?container =document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('buttons').getElementsByTagName('span');//獲取5個按鈕
var prev=document.getElementById('prev');//上一個
var next=document.getElementById('next');//下一個
var index=1;
var timer;
/*點亮小圓點*/
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 animate(offset){//封裝按鈕左右點擊,傳參offset
var newLeft=parseInt(list.style.left)+offset;
var time=300;//位移總時間
var interval=10;//位移間隔
var speed=offset/(time/interval);//每次位移量
function go(){
? ? ? ? ? ? ? ?if((speed<0 && parseInt(list.style.left)>newLeft) || (speed >0&&parseInt(list.style.left)<newLeft)){
? ? ? ? ? ? ? ? ? ?list.style.left=parseInt(list.style.left)+speed+'px';
/*定時器*/
setTimeout(go,interval);
}
? ? ? ? ? ? ? ?else{
? ? ? ? ? ? ? ? ? ?list.style.left=newLeft+'px';
/* 判斷是否滾動到附屬圖上,滾動到附屬圖上歸位*/
if(newLeft> -600){
? ? ? ? ? ? ? ? ? ? ? ?list.style.left = -3000 + 'px';
}
? ? ? ? ? ? ? ? ? ?if(newLeft< -3000){
? ? ? ? ? ? ? ? ? ? ? ?list.style.left = -600 + 'px';
}
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ? ? ?go();
}
? ? ? ?function play(){
? ? ? ? ? ?timer=setInterval(function (){
? ? ? ? ? ? ? ?next.onclick();
},3000);
}
? ? ? ?function stop(){
? ? ? ? ? ?clearInterval(timer);
}
? ? ? ?next.onclick=function (){
? ? ? ? ? ?if(index==5){
? ? ? ? ? ? ? ?index=1;
}
? ? ? ? ? ?else{
? ? ? ? ? ? ? ?index +=1;
}
? ? ? ? ? ? ? ? ? ?showButton();
animate(-600);//點擊右箭頭-600
}
? ? ? ?prev.onclick=function (){
? ? ? ? ? ?if(index==1){
? ? ? ? ? ? ? ?index=5;
}
? ? ? ? ? ?else{
? ? ? ? ? ? ? ?index -= 1;
}
? ? ? ? ? ?showButton();
animate(-600);//點擊左箭頭600
}
? ? ? ?for(var i=0;i<buttons.length;i++){
? ? ? ? ? ?//原始值
buttons[i].onclick=function (){
? ? ? ? ? ? ? ?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;
}
</script>
</body>
</html>