明霞山资源网 Design By www.htccd.com
用原生js实现无缝轮播图,供大家参考,具体内容如下
index.js:
var config = {
imgWidth:380,//图片尺寸
dotWidth:8,//小圆点尺寸
doms:{
divImgs:document.querySelector('.imgs'),
divDots:document.querySelector('.circle'),
divDirection:document.querySelector('.direction'),
divContainer:document.querySelector('.container')
},
curIndex:0,//实际图片索引,0 ~ imgNumber-1
timer:{
duration:16,//运动间隔时间
total:1000,//总时间
id:null//计时器编号
}
}
//图片的数量
config.imgNumber = config.doms.divImgs.children.length;
//初始化元素尺寸
config.imgsWidth = (config.imgNumber + 2)*config.imgWidth;
config.dotsWidth = (config.imgNumber + 2)*config.dotWidth;
//初始化
function inti(){
intiWidth();
intiCount();
intiElement();
intiPosition();
function intiWidth(){
config.doms.divImgs.style.width = config.imgsWidth + 'px';
config.doms.divDots.style.width = config.dotsWidth + 'px';
}
function intiCount(){
for(var i = 0; i < config.imgNumber; i ++){
var p = document.createElement('p');
config.doms.divDots.appendChild(p);
}
}
function intiElement(){
var first = config.doms.divImgs.children[0],last = config.doms.divImgs.children[config.imgNumber-1];
var newImg = first.cloneNode(true);//深度克隆
config.doms.divImgs.appendChild(newImg);
newImg = last.cloneNode(true);
config.doms.divImgs.insertBefore(newImg,first);
}
function intiPosition(){
var left = (-config.curIndex-1)*config.imgWidth;
config.doms.divImgs.style.marginLeft = left + 'px';
setDots();//小圆点的激活状态位置设置
}
}
inti();
//小圆点的激活状态位置设置
function setDots(){
for(var i = 0; i < config.doms.divDots.children.length; i++){
var dot = config.doms.divDots.children[i];
if(i === config.curIndex){
dot.className = 'select';
}else{
dot.className = '';
}
}
}
/*
图片切换
index: 图片索引
directions: 图片切换方向(left,right)
*/
function switchTo(index,directions){
if(index === config.curIndex){
return;
}
if(!directions){
directions = 'right';//默认状态下向右切换图片
}
//最终的显示图片; 图片容器的marginLeft
var newLeft = (-index-1)*config.imgWidth;
animateSwitch();
//config.doms.divImgs.style.marginLeft = newLeft + 'px';
//小圆点的激活状态位置设置
config.curIndex = index;
setDots();
//一张图片的总运动次数
var number = Math.ceil(config.timer.total/config.timer.duration);
//当前运动次数
var curNumber = 0;
var distance,//总运动距离
totalWidth = config.imgNumber*config.imgWidth,
marginLeft = parseFloat(getComputedStyle(config.doms.divImgs).marginLeft);
if(directions === 'left'){
if(newLeft < marginLeft){
distance = newLeft - marginLeft;
}else{
distance = -(totalWidth-Math.abs(newLeft - marginLeft));
}
}
if(directions === 'right'){
if(newLeft > marginLeft){
distance = newLeft - marginLeft;
}else{
distance = totalWidth-Math.abs(newLeft - marginLeft);
}
}
//每次改变的距离
var everDistence = distance/number;
//逐步改变marginLeft
function animateSwitch(){
clearAnimate();
config.timer.id = setInterval(function(){
marginLeft += everDistence;
if(directions === 'left' && Math.abs(marginLeft) > totalWidth){
marginLeft += totalWidth;
}
else if(directions === 'right' && Math.abs(marginLeft) < config.imgWidth){
marginLeft -= totalWidth;
}
config.doms.divImgs.style.marginLeft = marginLeft + 'px';
curNumber ++;
if(curNumber === number){
clearAnimate();
}
},config.timer.duration);
}
//清空计时器
function clearAnimate(){
clearInterval(config.timer.id);
config.timer.id = null;
}
}
//默认情况下自动向右轮播图片
var timer = setInterval(function(){
toRight();
},2000);
config.doms.divContainer.onmouseleave = function() {
timer = setInterval(function(){
toRight();
},2000);
}
//鼠标移出则清空定时器
config.doms.divContainer.onmouseover = function() {
clearInterval(timer);
}
//左右点击事件
config.doms.divDirection.onclick = function(e){
clearInterval(timer);
if(e.target.classList.contains('left')){
toLeft();
}
if(e.target.classList.contains('right')){
toRight();
}
}
function toLeft(){
var index = config.curIndex - 1;
if(index < 0){
index = config.imgNumber - 1;
}
switchTo(index,'right');
}
function toRight(){
var index = config.curIndex + 1;
if(index > config.imgNumber - 1){
index = 0;
}
switchTo(index,'left');
}
//小圆点点击事件
config.doms.divDots.onclick = function(e){
if(e.target.tagName === 'P'){
var index = Array.from(this.children).indexOf(e.target);
switchTo(index,index > config.curIndex"htmlcode">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>无缝轮播图</title>
<link rel="stylesheet" href="index.css" rel="external nofollow" >
</head>
<body>
<div class="container">
<div class="imgs">
<img class="item" src="/UploadFiles/2021-04-02/1.jpg">
index.css:
.container{
width:380px;
height:250px;
border:1px solid;
margin:0 auto;
position:relative;
overflow: hidden;
}
.container .imgs{
}
.container .imgs .item{
width:380px;
height:250px;
display:block;
float:left;
top:0;
}
.container .circle{
position:absolute;
left:0;
right:0;
margin:0 auto;
background:rgba(0,0,0,.3);
bottom:8px;
border-radius:5px;
}
.container .circle p{
width:8px;
height:8px;
background:#fff;
border-radius:50%;
float:left;
margin:2px;
cursor:pointer;
}
.container .circle p.select{
background:#f40;
}
.container .direction .item{
background:rgba(0,0,0,.4);
position:absolute;
top:120px;
width:20px;
height:26px;
padding:2px;
box-sizing:border-box;
display:none;
cursor:pointer;
}
.container .direction .item:hover{
background:rgba(0,0,0,.5);
}
.container:hover .direction .item{
display:block;
}
.container .direction .left{
left:0;
border-radius:0 15px 15px 0;
}
.container .direction .right{
right:0;
padding-left:6px;
border-radius:15px 0 0 15px;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。