> 效果如圖

> html代碼
```
var container = document.getElementById("container");
var list = document.getElementById("list");
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var btns = document.getElementById("btns").children;
var index = 0;
var animated = false;
/* ---------------------------prev, next點擊功能------------------ */
prev.onclick = function () {
if(animated) {
return false; //當animated為true時,prev并不會執行
}
animate(600);
index--;
showBtn();
}
next.onclick = function () {
if(animated) {
return false;
}
animate(-600);
index++;
showBtn();
}
/* -------------------------焦點點擊功能-------------------- */
for(let i=0; i<btns.length; i++) {
btns[i].onclick = function(){
/* animate偏移量為點擊的i-原先的index */
animate((i-index)*-600);
/* 此時的index等于點擊的i值 */
index = i;
showBtn();
}
}
/* ----------------------自動播放---------------------------- */
function autoplay() {
var timer2 = setInterval(function(){
next.onclick();
}, 800);
}
function stopplay() {
clearInterval(timer2);
}
autoplay();
/* ----------------------鼠標懸停播放停止---------------------- */
container.onmouseover = stopplay;
container.onmouseout = autoplay;
function animate(offset) {
var newLeft = parseInt(list.style.left) + offset; //number類型
var timer = 300; //便宜一次總共用時300ms
var interval = 10; //每隔10ms執行一次
var speed = offset / (timer / interval); //每次偏移的speed為20px
function go() {
animated = true;
var leftValue = parseInt(list.style.left);
if((speed<0 && leftValue>newLeft) || (speed>0 && leftValue<newLeft)){
//如果速度小于0(向左偏移),并且此時left的至大于設定偏移的距離
//或者如果速度大于0(向右偏移),并且此時left的值小于設定偏移的距離
list.style.left = leftValue + speed + "px";
//當前的left值+speed
setTimeout(go, interval); //每隔10ms執行一次go(每10ms,偏移20px)
}
/* 當前left值等于設定的newLeft后 */
else {
animated = false; //animated回到false,點擊事件可以執行
if(newLeft<-3000) {
newLeft = -600;
list.style.left = newLeft + "px";
}
if(newLeft>-600) {
newLeft = -3000;
list.style.left = newLeft + "px";
}
}
/* 每隔10ms list的left增加一個speed */
}
go();
console.log("目標偏移量:"+newLeft);
}
function showBtn() {
/* 先移除所有current */
for(i=0; i<btns.length; i++) {
btns[i].classList.remove("current");
}
if(index<0){
index = 4;
}
if(index>4){
index = 0;
}
/* 給當前index加上current */
btns[index].classList.add("current");
}
```
----
整個demo以上傳至[github](https://github.com/MrXuxu/H5_demo/tree/master/%E8%BD%AE%E6%92%AD%E5%9B%BEsetTimout%E7%89%88)
- 空白目錄
- css實用樣式
- css--下拉欄的幾種設計
- css--圖片陰影以及浮起的效果
- css--圖片翻轉二:自動翻轉
- css--圖片翻轉一:滑過翻轉
- css--三種loading特效
- css--圖片遮罩效果實現
- css--又是三種loading特效
- css--帶三角形的圖形實現
- js demo
- 原生demo
- 1. 原生js實現輪播圖
- 2. 倒計時按鈕
- 3. 動態添加表格
- 4. checkbox全選反選
- 5. 小米登錄方式切換
- 6. 點擊事件
- 7. 個人網頁導航條(二)點擊滾動
- 8. 瀑布流實現!
- 9. 個人網頁導航條(一)滑動固定
- 10. 定時器實現淡入淡出效果
- 11. 輪播圖setTimeout版
- jQuery demo
- 1. 輪播圖實現!
- 2. 成都小風車導航特效
- html組件
- html--導航欄(家居醫生)
- html--登錄頁面(小米登錄)
- html--響應式導航條(木兮地板)
- html--搜索欄
- Vue demo
- 1. mvvm實現動態添加表格
- 2. 豆瓣TOP250渲染
- 3. 制作一段跑馬燈文字
- 3.1. vue 單行文字自動跑馬燈效果
- 4. 利用豆瓣接口搜索書籍
- 5. 制作簡易計算器
- 6. 創建一個點贊組件
- 7. 列表添加刪除動畫
- 8. isShow手風琴原理
- 9. tab欄切換