原生js實(shí)現(xiàn)旋轉(zhuǎn)木馬輪播圖效果
更新時(shí)間:2017年02月27日 16:24:12 作者:chang紅達(dá)
本文主要介紹了原生js實(shí)現(xiàn)旋轉(zhuǎn)木馬輪播圖效果的實(shí)例,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
話不多說,請(qǐng)看代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>旋轉(zhuǎn)木馬特效</title>
<style type="text/css">
*{margin: 0;padding: 0;list-style:none;}
#demo{width:1200px;margin:100px auto 0;}
#innerht{width:1200px;height:500px;position:relative;}
ul li{position:absolute;top:0;left:0;z-index:1;}
ul li img{width:100%;}
#corrow{position:absolute;width:100%;top:50%;opacity:0;z-index:99;}
#btn_lef,#btn_rig{position:absolute;height:112px;width:76px;top:50%;margin-top:-56px;}
#btn_lef{left:0;}
#btn_rig{right:0;}
</style>
</head>
<body>
<div id="demo">
<div id="innerht">
<ul>
<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=dac666f84f6ad05ec026ad655ea0f159" alt=""></a></li>
<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=89c2213f2b614db88b5724b82dafc02a" alt=""></a></li>
<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=e7969486909c076abf795995bdc3da54" alt=""></a></li>
<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=b404e53a3f4dd7e97438693e8e2d4082" alt=""></a></li>
<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=ed309dded163d172b53154b2046eb1a2" alt=""></a></li>
</ul>
<div id="corrow">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="btn_lef"><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=206927b03b97b194bc04cdcaf0e10a33" height="112" width="76" alt=""></a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="btn_rig"><img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=0e84253cccdf667e293522a0a107eeec" height="112" width="76" alt=""></a>
</div>
</div>
</div>
</body>
<script type="text/javascript">
(function(window){
function $(id){
return document.getElementById(id);
};
//每張圖片對(duì)應(yīng)的樣式
var tempArr = [
{
"width":400,
"top":20,
"left":50,
"opacity":0.2,
"zIndex":2
},
{
"width":600,
"top":70,
"left":0,
"opacity":0.8,
"zIndex":3
},
{
"width":800,
"top":100,
"left":200,
"opacity":1,
"zIndex":4
},
{
"width":600,
"top":70,
"left":600,
"opacity":0.8,
"zIndex":3
},
{
"width":400,
"top":20,
"left":750,
"opacity":0.2,
"zIndex":2
}
];
// 設(shè)置限流函數(shù)
var onOff = true ;
// 獲取對(duì)象
var left = $("btn_lef"),right = $("btn_rig"),innerht = $("innerht"),ul = innerht.children[0],lis = ul.children,arrow = $("corrow");
// 給每個(gè)li添加樣式
addStyle();
// 大盒子的hover事件
innerht.onmouseover = function(){
animate(arrow,{"opacity":1});
};
innerht.onmouseout = function(){
animate(arrow,{"opacity":0});
};
// 右箭頭點(diǎn)擊事件
right.onclick = function(){
if( onOff ){
onOff = false;
var atop = tempArr.shift();
tempArr.push( atop );
addStyle();
};
};
// 左箭頭點(diǎn)擊事件
left.onclick = function(){
if( onOff ){
onOff = false;
var apop = tempArr.pop();
tempArr.unshift( apop );
addStyle();
};
};
function addStyle(){
for( i = 0 ; i < lis.length ; i++ ){
animate(lis[i],tempArr[i],function(){
onOff = true;
console.log(onOff);
});
};
};
// 設(shè)置animate函數(shù)
function animate(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var flog = true ;
for( k in json ){
if( k === "zIndex" ){
obj.style[k] = json[k];
}else if( k === "opacity" ){
var leader = getStyle(obj,k) * 100 ;
var target = json[k] * 100 ;
var step = ( target - leader ) / 10 ;
step = step > 0 ? Math.ceil( step ) : Math.floor( step ) ;
// 浮點(diǎn)數(shù)判斷相等時(shí)是不準(zhǔn)確的
// 所以不能寫leader = ( leader + step )/100;
// 保持leader和target都是整數(shù),便于下面判斷相等
leader = leader + step ;
obj.style[k] = leader / 100;
}else{
var leader = parseInt( getStyle(obj,k) ) || 0 ;
var target = json[k];
var step = ( target - leader ) / 10 ;
step = step > 0 ? Math.ceil( step ) : Math.floor( step ) ;
leader = leader + step ;
obj.style[k] = leader + "px";
};
if( leader !== target ){
flog = false ;
};
};
console.log(flog);
if( flog ){
clearInterval( obj.timer );
if( fn ){
fn();
};
};
}, 15);
};
// 設(shè)置getStyle函數(shù),獲取計(jì)算后的樣式
function getStyle(obj,attr){
if( window.getComputedStyle ){
return window.getComputedStyle(obj,null)[attr];
}else{
return obj.currentStyle[attr];
};
};
})(window)
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
利用純js + transition動(dòng)畫實(shí)現(xiàn)移動(dòng)端web輪播圖詳解
這篇文章主要給大家介紹了利用純js + transition動(dòng)畫實(shí)現(xiàn)移動(dòng)端web輪播圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
JavaScript實(shí)現(xiàn)網(wǎng)頁播放器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
JS實(shí)現(xiàn)點(diǎn)擊顏色塊切換指定區(qū)域背景顏色的方法
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊顏色塊切換指定區(qū)域背景顏色的方法,涉及javascript操作cookie及背景色的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
Bootstrap 填充Json數(shù)據(jù)的實(shí)例代碼
本篇文章主要介紹了Bootstrap 填充Json數(shù)據(jù)的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
JavaScript程序設(shè)計(jì)之JS調(diào)試
這篇文章主要介紹了JavaScript程序設(shè)計(jì)中的重要環(huán)節(jié):JS調(diào)試,本文通過一個(gè)加法器,介紹JS如何調(diào)試,感興趣的小伙伴們可以參考一下2015-12-12

