簡單的漸變輪播插件
更新時間:2017年01月12日 15:13:19 作者:Creabine
本文主要介紹了簡單的漸變輪播插件,具有一定的參考價值,下面跟著小編一起來看下吧
話不多說,請看代碼:
<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>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
您可能感興趣的文章:
- jQuery簡單自定義圖片輪播插件及用法示例
- jQuery自適應(yīng)輪播圖插件Swiper用法示例
- jQuery的圖片輪播插件PgwSlideshow使用詳解
- Bootstrap 最常用的JS插件系列總結(jié)(圖片輪播、標(biāo)簽切換等)
- Bootstrap輪播插件中圖片變形的終極解決方案 使用jqthumb.js
- BootStrap 輪播插件(carousel)支持左右手勢滑動的方法(三種)
- 第十篇BootStrap輪播插件使用詳解
- jQuery插件實現(xiàn)圖片輪播特效
- jQuery圖片輪播插件——前端開發(fā)必看
- jquery插件tytabs.jquery.min.js實現(xiàn)漸變TAB選項卡效果
相關(guān)文章
你不知道的 TypeScript 高級類型(小結(jié))
這篇文章主要介紹了你不知道的 TypeScript 高級類型(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Firefox中beforeunload事件的實現(xiàn)缺陷淺析
beforeunload 指在頁面卸載前提供的最后一次JS執(zhí)行的機(jī)會2012-05-05
基于JavaScript實現(xiàn)購物網(wǎng)站商品放大鏡效果
大家在日常生活中都有網(wǎng)購的經(jīng)驗,有的網(wǎng)站會有商品放大鏡功能,效果非常棒,那么基于js代碼是如何實現(xiàn)的呢?下面小編給大家?guī)砹嘶趈s實現(xiàn)購物網(wǎng)站商品放大鏡效果,非常不錯,感興趣的朋友參考下吧2016-09-09

