面向对象无缝滚动轮播(附带面向过程代码)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>banner</title>
<link rel="stylesheet" href="CSS/main1.css">
</head>
<body>
<div class="banner-wrapper" id="banner-wrapper">
<div class="banner" id="banner">
<img src="images/8.jpg" alt="假象图">
<img src="images/1.jpg" alt="广告图">
<img src="images/2.jpg" alt="广告图">
<img src="images/3.jpg" alt="广告图">
<img src="images/4.jpg" alt="广告图">
<img src="images/5.jpg" alt="广告图">
<img src="images/6.jpg" alt="广告图">
<img src="images/7.jpg" alt="广告图">
<img src="images/8.jpg" alt="广告图">
<img src="images/1.jpg" alt="假象图">
</div>
<div class="btn prev" id="prev">&lt;</div>
<div class="btn next" id="next">&gt;</div>
<ul class="circle" id="circle">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script type="text/javascript" src="JS/object.js"></script>
</body>
</html>

CSS代码:

*{margin: 0;padding: 0}
body{background: #f3f5f7;font-size: 14px;}
ul{
list-style: none;
}
.banner-wrapper{
width: 520px;height: 280px;position: relative;margin: 50px auto;border:3px solid #999; overflow: hidden;
}
.banner{
position: absolute;width: 5200px;left: -520px;
}
.banner img{
float: left;
}
//图片
// .banner-wrapper .btn{position: absolute;top: 50%;width: 35px;height: 80px;margin-top: -40px;background: url(../images/btn.png) no-repeat;display: none;opacity: 0.6;}
// .banner-wrapper:hover .btn{display: block;}
// .banner-wrapper .btn:hover{opacity: 1;}
// .banner-wrapper .prev{left: 0;background-position: -73px 0; }
// .banner-wrapper .next{right: 0;background-position: 0 0;}
.banner-wrapper .btn{position: absolute;top: 50%;width: 35px;height: 70px;line-height: 60px;margin-top: -35px;text-align: center;background:#000;opacity: 0.6;font-size: 36px;color: #fff;display: none;cursor: pointer;}
.banner-wrapper:hover .btn{display: block;}
.banner-wrapper .btn:hover{opacity: 0.9;}
.banner-wrapper .prev{left: 0;border-radius: 0 32px 32px 0;}
.banner-wrapper .next{right: 0;border-radius: 32px 0 0 32px;}
.banner-wrapper .circle{position: absolute;bottom: 20px;left: 35%;background: hsla(0,0%,100%,0.4);padding: 5px;border-radius: 12px;}
.banner-wrapper .circle li{width: 15px;height:15px;background: #fff;float: left;border-radius: 50%;margin:0 4px;cursor: pointer;}
.banner-wrapper .circle li.active{background: orange;}

 

面向对象js代码

window.onload = function() {
new Banner('banner-wrapper')
}

function Banner(id) {
var _this = this
var bannerWrapper = document.getElementById(id)
this.banner = document.getElementById('banner')
this.oimg = banner.getElementsByTagName('img')[0]
this.imglen = banner.getElementsByTagName('img').length
this.oimgW = this.oimg.clientWidth
this.prevBtn = document.getElementById('prev')
this.nextBtn = document.getElementById('next')
this.circle = document.getElementById('circle')
this.ali = circle.getElementsByTagName('li')
this.num = 1;
this.Timer = null;

aliActive = function() {
_this.aliActive(this)
}

imgChange = function() {
_this.imgChange(this)
}
autoPlay = function() {
_this.autoPlay(this)
}

//下一张
this.nextBtn.onclick = function() {
_this.imgChange(this)
}
//上一张
this.prevBtn.onclick = function() {
_this.prevClick(this)
}

// 鼠标移入时清除定时器
bannerWrapper.onmouseover = function() {
clearInterval(_this.Timer);
}
// 鼠标移出时继续播放
bannerWrapper.onmouseout = function() {
_this.autoPlay(this)
}
// 启动定时器
_this.autoPlay(this)

/*滑动悬浮下标小圆点进行切换*/
for (var i = 0; i < this.ali.length; i++) { //遍历li

this.ali[i].index = i; //下标索引
this.ali[i].onmouseover = function() {
_this.aliOnmouseover(this)
}
}
}

Banner.prototype.prevClick = function() {
var _this = this
this.num--;
if (this.num == 0) {
this.num = 9;
this.banner.style.left = -this.num * this.oimgW + 'px';
this.banner.style.transition = 'all 0s';
this.banner.style.webkitTransition = 'all 0s';
this.num = 8;
}

setTimeout(function() {
_this.Transition(this)
}, 0);
aliActive();
}

Banner.prototype.Transition = function() {
this.banner.style.left = -this.num * this.oimgW + 'px';
this.banner.style.transition = 'all 1s';
this.banner.style.webkitTransition = 'all 1s';
}

Banner.prototype.imgChange = function() {
var _this = this
this.num++;
if (this.num == 9) {
this.num = 0;
this.banner.style.left = -this.num * this.oimgW + 'px';
this.banner.style.transition = 'all 0s';
this.banner.style.webkitTransition = 'all 0s';
this.num = 1;
}
setTimeout(function() {
_this.Transition(this)
}, 0);
_this.aliActive(this)
}

Banner.prototype.aliActive = function(oLi) {
for (var i = 0; i < this.ali.length; i++) {
this.ali[i].className = '';
}
this.ali[this.num - 1].className = 'active';
}

Banner.prototype.autoPlay = function() {
var _this = this
_this.Timer = setInterval(function() {
_this.imgChange(this)
}, 2000);
}

Banner.prototype.aliOnmouseover = function(oLi) {
for (var j = 0; j < this.ali.length; j++) {
this.ali[j].className = '';
}
oLi.className = 'active';
this.num = oLi.index + 1; //由于图片跟索引下标相差1 因此+1
this.banner.style.left = -this.num * this.oimgW + 'px';
this.banner.style.transition = 'all 1s';
this.banner.style.webkitTransition = 'all 1s';

}

面向过程js代码

window.onload = function() {

var bannerWrap = document.getElementById('banner_wrap');
var banner = document.getElementById('banner');
var prevBtn = document.getElementById('prev');
var nextBtn = document.getElementById('next');
var oimg = banner.getElementsByTagName('img')[0];
var oimgW = oimg.clientWidth; //获取一张图片的宽度
var cir = document.getElementById('circle');
var ali = cir.getElementsByTagName('li'); //获取li的集合
/*var num = 0;*/ //num从0开始记 0 1 2 3...6 7 没有假象图初始化为0
var num = 1; //由于前面添加了假象图 因此初始化为1;
var len = banner.getElementsByTagName('img').length; // 图片的数量

/*封装函数*/
function imgChange() {
// banner.style.left = -oimgW + 'px';//移动一张图片 添加一个变量可改变n张图片的宽度
num++;
// if (num == 8) { //对num的值进行判断
// num = 0;
// }
if (num == 9) { //由于假象图 初始值为1,因此判断要加1;
num = 0;
banner.style.left = -num * oimgW + 'px'; //跳回假象图
banner.style.transition = 'all 0s'; //跳回假象图 不要过度效果
banner.style.webkitTransition = 'all 0s';
num = 1; //从假象图到真1图 实现有过渡效果 不加上num=1 会出现undefined
}
setTimeout(function() { //内部函数语句
banner.style.left = -num * oimgW + 'px'; //移动图片的宽度进行位移
banner.style.transition = 'all 1s'; //过渡动画
banner.style.webkitTransition = 'all 1s';
}, 0)
// setTimeout 运行机制 先执行主线程里的语句,之后再执行内部函数里面的语句
/*banner.style.left = -num * oimgW + 'px'; //移动图片的宽度进行位移
banner.style.transition = 'all 1s'; //过渡动画
banner.style.webkitTransition = 'all 1s';*/
/*js dom 操作的合并机制 因此引入定时器 实现假象图跳转真图无过渡效果*/

// for (var i = 0; i < ali.length; i++) { //消除其他li的样式
// ali[i].className = '';
// }
// 由于初始值改为1,因此num要-1才能让小标圆点对应上
// /*ali[num].className = 'active';*/
// ali[num - 1].className = 'active';
aliActive();

//给移动的图片添加active样式 同时也要消除其他li的样式 因此用for循环
}

/*封装aliActive*/
function aliActive() {
for (var i = 0; i < ali.length; i++) {
ali[i].className = '';
}
/* ali[this.index].className = 'active'; //this.index == num-1*/
ali[num - 1].className = 'active';
}

/*切换下一张*/
nextBtn.onclick = function() {
imgChange();
}

 

/*切换上一张*/
prevBtn.onclick = function() {
num--;
if (num == 0) {
num = 9;
banner.style.left = -num * oimgW + 'px';
banner.style.transition = 'all 0s';
banner.style.webkitTransition = 'all 0s';
num = 8;
}
setTimeout(function() {
banner.style.left = -num * oimgW + 'px';
banner.style.transition = 'all 1s';
banner.style.webkitTransition = 'all 1s';
}, 0);
aliActive();
}

/*定时切换*/
var Timer = null;

function Move() {

Timer = setInterval(function() {
imgChange();
}, 2000)
}
/*封装的函数不会自动执行 因此要启动定时器*/
Move();

/*鼠标滑过清除滑动(定时器)*/
bannerWrap.onmouseover = function() {
clearInterval(Timer);
}
/*鼠标移出继续滑动(定时器) 因此封装定时切换*/
bannerWrap.onmouseout = function() {
Move();
}

/*滑动悬浮下标小圆点进行切换*/
for (var i = 0; i < ali.length; i++) { //遍历li
ali[i].index = i; //下标索引
ali[i].onmouseover = function() {
num = this.index + 1; //由于图片跟索引下标相差1 因此+1
aliActive();

banner.style.left = -num * oimgW + 'px';
banner.style.transition = 'all 1s';
banner.style.webkitTransition = 'all 1s';
}
}
}

代码演示地址: https://liangtianfu.github.io/ObjectBanner/objectbanner.html

转载于:https://www.cnblogs.com/LiangTianFu/p/8035300.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中的轮图无缝滚动通常使用轮插件或自定义组件来实现,常见的库如`vue-awesome-swiper`或者`vue-slick-carousel`。以下是一个简单的`vue-awesome-swiper`的无缝滚动示例代码: ```html <template> <swiper :options="swiperOptions"> <swiper-slide v-for="(item, index) in items" :key="index"> <img :src="item.src" :alt="item.alt"> </swiper-slide> <!-- 如果你想添加导航按钮 --> <div class="swiper-button-prev" @click="prevSlide"></div> <div class="swiper-button-next" @click="nextSlide"></div> </swiper> </template> <script setup> import { ref, onMounted } from 'vue'; import { swiper, SwiperSlide } from 'vue-awesome-swiper'; const swiperOptions = ref({ // 设置轮图选项,例如自动放、循环、速度等 loop: true, // 开启无缝滚动 autoplay: { delay: 3000, // 每隔3秒切换到下一张 }, pagination: { el: '.swiper-pagination', }, }); let currentSlide = ref(0); // 当前索引 onMounted(() => { // 初始化Swiper实例 const swiperInstance = swiper(swiperOptions.value); // 监听滑动事件 swiperInstance.on('slideChangeTransitionEnd', () => { currentSlide.value = swiperInstance.realIndex; // 更新当前索引 }); }); // 方法用于切换到上一张和下一张 function prevSlide() { if (currentSlide.value > 0) { currentSlide.value--; } } function nextSlide() { const slidesCount = swiperOptions.value.slides.length; if (currentSlide.value + 1 < slidesCount) { currentSlide.value++; } } </script> <style scoped> .swiper-button-prev, .swiper-button-next { /* 根据你的样式需求进行定制 */ } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值