課程
/前端開發(fā)
/JavaScript
/焦點(diǎn)圖輪播特效
誰(shuí)有完整的代碼,跟著敲為什么會(huì)有那么大的差異?
2018-11-11
源自:焦點(diǎn)圖輪播特效 5-1
正在回答
window.onload=function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('button').getElementsByTagName('span');
var prev=document.getElementById('prev');
var next=document.getElementById('next');
var index=1;?
var animated=false;
? ? //按鈕高亮
? ? function showButton(){
? ? for (var i = 0; i < buttons.length; i++) {
? ? buttons[i].className='';
? ? }
? ? buttons[index-1].className='on';
? ? //驅(qū)動(dòng)left的變化,切換圖片
? ? function animate(offset){
? ? animated=true;
? ? var newLeft=parseInt(list.style.left)+offset;
? ? ? ? var time=200;//位移總時(shí)間
? ? ? ? var interval=10;//位移間隔時(shí)間
? ? ? ? var speed =offset/(time/interval);//每次位移量
? ? ? ? //位移之前的判斷函數(shù),是否位移
? ? ? ? function go(){
? ? ? ? //向左移動(dòng)&&獲取值大于目標(biāo)值
? ? ? ? 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{
? ? ? ? animated=false;
? ? ? ? list.style.left=newLeft+'px';
? ? ? ? if (newLeft>-600) {
? ? ? ? list.style.left=-3000+'px';
? ? ? ? }
? ? ? ? if(newLeft<-3000){
? ? ? ? list.style.left=-600+'px';
? ? ? ? }?
? ? ? ? go();
? ? }?
? ? ? ? //右按鈕單擊
? ? ? ? next.onclick=function(){
? ? ? ? if (index==5) {
? ? ? ? index=1;
? ? ? ? index+=1;
? ? ? ? }? ? ?
? ? ? ? showButton();
? ? ? ? if (!animated) {
? ? ? ? ? animate(-600);
? ? ? ? //左按鈕單擊
? ? ? ? prev.onclick=function(){
? ? ? ? if (index==1) {
? ? ? ? index=5
? ? ? ? index-=1;
? ? ? ? animate(+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);
? ? ? ? index = myIndex;
? ? ? ? animate(offset);
舉報(bào)
通過(guò)本教程學(xué)習(xí)您將能掌握非常實(shí)用的焦點(diǎn)圖輪播特效的制作過(guò)程
1 回答和老師跟著寫的,運(yùn)行不了
1 回答為啥這一節(jié)我跟著老師寫,不管怎么寫,總是運(yùn)行不了?
1 回答這個(gè)輪播效果為啥出不來(lái)呢 跟著他寫的
1 回答敲完第一部分next不移動(dòng)圖片
1 回答為什么我的箭頭和小圓點(diǎn)跟著第一張圖片跑?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-11-23
window.onload=function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('button').getElementsByTagName('span');
var prev=document.getElementById('prev');
var next=document.getElementById('next');
var index=1;?
var animated=false;
? ? //按鈕高亮
? ? function showButton(){
? ? for (var i = 0; i < buttons.length; i++) {
? ? buttons[i].className='';
? ? }
? ? buttons[index-1].className='on';
? ? }
? ? //驅(qū)動(dòng)left的變化,切換圖片
? ? function animate(offset){
? ? animated=true;
? ? var newLeft=parseInt(list.style.left)+offset;
? ? ? ? var time=200;//位移總時(shí)間
? ? ? ? var interval=10;//位移間隔時(shí)間
? ? ? ? var speed =offset/(time/interval);//每次位移量
? ? ? ? //位移之前的判斷函數(shù),是否位移
? ? ? ? function go(){
? ? ? ? //向左移動(dòng)&&獲取值大于目標(biāo)值
? ? ? ? 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{
? ? ? ? animated=false;
? ? ? ? list.style.left=newLeft+'px';
? ? ? ? if (newLeft>-600) {
? ? ? ? list.style.left=-3000+'px';
? ? ? ? }
? ? ? ? if(newLeft<-3000){
? ? ? ? list.style.left=-600+'px';
? ? ? ? }
? ? ? ? }
? ? ? ? }?
? ? ? ? go();
? ? }?
? ? ? ? //右按鈕單擊
? ? ? ? next.onclick=function(){
? ? ? ? if (index==5) {
? ? ? ? index=1;
? ? ? ? }else{
? ? ? ? index+=1;
? ? ? ? }? ? ?
? ? ? ? showButton();
? ? ? ? if (!animated) {
? ? ? ? ? animate(-600);
? ? ? ? }
? ? ? ? }
? ? ? ? //左按鈕單擊
? ? ? ? prev.onclick=function(){
? ? ? ? if (index==1) {
? ? ? ? index=5
? ? ? ? }else{
? ? ? ? index-=1;
? ? ? ? }
? ? ? ? showButton();
? ? ? ? if (!animated) {
? ? ? ? animate(+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);
? ? ? ? index = myIndex;
? ? ? ? showButton();
? ? ? ? if (!animated) {
? ? ? ? animate(offset);
? ? ? ? }
? ? ? ? }
? ? ? ? }
? ? }