簡(jiǎn)單的漸變輪播插件
更新時(shí)間:2017年01月12日 15:13:19 作者:Creabine
本文主要介紹了簡(jiǎn)單的漸變輪播插件,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
話不多說(shuō),請(qǐng)看代碼:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
<style>
.CreabineCarousel{
width: 100%;
height: 700px;
background-size: cover;
position: relative;
}
.CreabineCarousel .CreabineCarousel-dotContainer{
position:absolute;
bottom: 5%;
margin:0 auto;
z-index: 100;
list-style-type: none;
width: 100%;
text-align: center;
left: 0;
padding: 0;
}
.CreabineCarousel .CreabineCarousel-dotContainer .dot{
width: 30px;
height: 4px;
border-radius:3px;
background-color:#fff;
display: inline-block;
margin:0 5px;
opacity: 0.7;
}
.CreabineCarousel .CreabineCarousel-dotContainer .dot:hover{
opacity: 1;
}
.CreabineCarousel .CreabineCarousel-item{
position:absolute;
width: 100%;
height: 100%;
transition:all 0.8s;
}
.CreabineCarousel .CreabineCarousel-item h1{
max-width: 600px;
text-align: center;
font-size: 5rem;
line-height: 1.3;
color: #fff;
padding: 300px 50px 0 50px;
margin:0 auto;
}
.CreabineCarousel .CreabineCarousel-item p{
max-width: 600px;
text-align: center;
font-size: 1.4rem;
line-height: 1.4;
color: #fff;
padding-top: 10px 50px 0 50px;
margin:0 auto;
}
</style>
</head>
<body>
<div id="carouselRoot"></div>
<script>
function CreabineCarousel(options){
var imgPathList = options.images;
var textList = options.contant;
if (!options.root) {
throw "require root to this CreabineCarousel";
}
if (!imgPathList) {
throw "must provide parameter images";
}
if (imgPathList.length != textList.length) {
throw "images are not equal to contants";
}
var changeCount = 0;
var timer;
var _autoScroll = options.autoScroll || false;
var _scrollDuration = options.scrollDuration || 4000;
var _height = options.height || 700;
function initElements() {
var _root = document.getElementById(options.root);
if (!_root) {
throw "no exist called this name element,please create element called this name";
}
_root.className = "CreabineCarousel";
_root.style.height = _height + "px";
var _dotContainer = document.createElement("ul");
_dotContainer.className = 'CreabineCarousel-dotContainer';
_root.appendChild(_dotContainer);
for (var i = 0; i < imgPathList.length; i++) {
var _dot = document.createElement("li");
_dot.className = "dot";
_dot.id = "item" + (i+1) + "dot";
_dotContainer.appendChild(_dot);
var _item = document.createElement("div");
_item.className = "CreabineCarousel-item"
_item.id = "item" + (i+1);
_item.style.backgroundImage = "url(" + imgPathList[i] + ")";
_item.style.backgroundSize = "cover";
_item.style.backgroundRepeat = "no-repeat";
if(i == 0){
_item.style.opacity = '0';
_item.style.zIndex = '1';
}
_root.appendChild(_item);
var _h = document.createElement("h1");
_h.innerText = textList[i].title;
_item.appendChild(_h);
var _p = document.createElement("p");
_p.innerText = textList[i].text;
_item.appendChild(_p);
}
_dotContainer.addEventListener("mouseover",function(e){
if( e.target && e.target.className == "dot" ){
clearInterval(timer);
var id = e.target.id.substring(0,5);
CarouselHover(id);
}
});
_dotContainer.addEventListener("mouseout",function(e){
if( e.target && e.target.className == "dot" ){
var id = e.target.id;
CarouselOut(id);
}
});
if(_autoScroll){
timer = setInterval(function(){Carousel()},_scrollDuration);
}
}
function Carousel(){
var all = document.getElementsByClassName('CreabineCarousel-item');
for (var i = all.length - 1; i >= 0; i--) {
all[i].style.opacity = '0';
all[i].style.zIndex = '1';
}
var i=((changeCount++%5)+1);
var id = "item" + i;
document.getElementById(id).style.opacity = '1';
document.getElementById(id).style.zIndex = '10';
}
function CarouselHover(id){
clearInterval(timer);
var all = document.getElementsByClassName('CreabineCarousel-item');
for (var i = all.length - 1; i >= 0; i--) {
all[i].style.opacity = '0';
all[i].style.zIndex = '1';
}
document.getElementById(id).style.opacity = '1';
document.getElementById(id).style.zIndex = '10';
}
function CarouselOut(id){
var num = id.substring(4,5);
num = parseInt(num)-1;
changeCount = num;
timer = window.setInterval(function(){Carousel()},_scrollDuration);
}
initElements();
new CreabineCarousel({
root:'carouselRoot',
autoScroll:true,
scrollDuration:3000,
height:700,
images:['https://cdn.worktile.com/images/index/index_all_bg_1.jpg?v=4.5.18','https://cdn.worktile.com/images/index/index_all_bg_2.jpg?v=4.5.18','https://cdn.worktile.com/images/index/index_all_bg_3.jpg?v=4.5.18','https://cdn.worktile.com/images/index/index_all_bg_4.jpg?v=4.5.18','https://cdn.worktile.com/images/index/index_all_bg_5.jpg?v=4.5.18'],
contant:[
{
title:"title-1",
text:"text-111"
},
{
title:"title-2",
text:"text-222"
},
{
title:"title-3",
text:"text-333"
},
{
title:"title-4",
text:"text-444"
},
{
title:"title-5",
text:"text-555"
},
]
});
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
您可能感興趣的文章:
- jQuery簡(jiǎn)單自定義圖片輪播插件及用法示例
- jQuery自適應(yīng)輪播圖插件Swiper用法示例
- jQuery的圖片輪播插件PgwSlideshow使用詳解
- Bootstrap 最常用的JS插件系列總結(jié)(圖片輪播、標(biāo)簽切換等)
- Bootstrap輪播插件中圖片變形的終極解決方案 使用jqthumb.js
- BootStrap 輪播插件(carousel)支持左右手勢(shì)滑動(dòng)的方法(三種)
- 第十篇BootStrap輪播插件使用詳解
- jQuery插件實(shí)現(xiàn)圖片輪播特效
- jQuery圖片輪播插件——前端開發(fā)必看
- jquery插件tytabs.jquery.min.js實(shí)現(xiàn)漸變TAB選項(xiàng)卡效果
相關(guān)文章
5秒后跳轉(zhuǎn)效果(setInterval/SetTimeOut)
實(shí)現(xiàn)5秒后自動(dòng)跳轉(zhuǎn)效果有兩種方式setInterval與SetTimeOut,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-05-05
js鼠標(biāo)按鍵事件和鍵盤按鍵事件用法實(shí)例匯總
這篇文章主要介紹了js鼠標(biāo)按鍵事件和鍵盤按鍵事件用法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript針對(duì)鼠標(biāo)與鍵盤事件的常用操作技巧,需要的朋友可以參考下2016-10-10
JS樹形菜單組件Bootstrap TreeView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了js組件Bootstrap TreeView使用方法,本文一部分針對(duì)于bootstrap的treeview的實(shí)踐,另一部分是介紹自己寫的樹形菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
關(guān)于JS中setTimeout()無(wú)法調(diào)用帶參函數(shù)問(wèn)題的解決方法
這篇文章主要介紹了關(guān)于JS中setTimeout()無(wú)法調(diào)用帶參函數(shù)問(wèn)題的解決方法,提供了2種解決方法供大家對(duì)比參考,需要的朋友可以參考下2016-06-06
微信小程序生成海報(bào)分享朋友圈的實(shí)現(xiàn)方法
利用微信強(qiáng)大的社交能力通過(guò)小程序達(dá)到裂變的目的,拉取新用戶。下面小編給大家?guī)?lái)了微信小程序生成海報(bào)分享朋友圈的實(shí)現(xiàn)方法,感興趣的朋友跟隨小編一起看看吧2019-05-05

