老师我的效果实现不了,也没想通这个轮播图,希望老师讲解一下
来源:2-3 代码编写(1)
慕仰5214815
2020-11-15 16:43:20
# 具体遇到的问题
首页动态项目慕家居的精品推荐部分轮播图
# 报错信息的截图
# 相关课程内容截图
# 尝试过的解决思路和结果
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
font-family: '微软雅黑';
}
ul,
ol {
list-style: none;
}
a {
text-decoration: none;
}
.center-wrap {
width: 1201px;
margin: 0 auto;
}
.recommend {
background-color: #EFF0F4;
height: 590px;
position: relative;
/* aboutCenter高度使用44px,剩余38 */
margin-top: 38px;
padding-top: 70px;
}
.recommend .center-wrap {
position: relative;
}
.recommend .center-wrap .similar {
width: 232px;
position: absolute;
left: 50%;
margin-left: -116px;
padding-top: 32px;
text-align: center;
}
.recommend .center-wrap .similar h3 {
font-size: 32px;
color: #696868;
font-weight: normal;
}
.recommend .center-wrap .similar span {
font-size: 18px;
color: #9B9B9B;
position: relative;
}
/* 列表 */
.recommend .center-wrap .carousel {
width: 1150px;
overflow: hidden;
}
.recommend .center-wrap .list {
overflow: hidden;
margin-left: 61px;
padding-top: 195px;
width: 10000px;
left: 0;
transition: left .5s ease 0s;
}
.recommend .center-wrap .list li {
float: left;
position: relative;
margin-right: 23px;
}
.recommend .center-wrap .list li a img {
width: 345px;
height: 326px;
}
.recommend .center-wrap .list li:last-child {
margin-left: 0;
}
.recommend .center-wrap .list li a .recomWords {
width: 345px;
height: 45px;
position: absolute;
left: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.50);
font-size: 16px;
color: #FFFFFF;
line-height: 45px;
text-align: center;
}
/* 左右箭头图标*/
.recommend .arrow {
width: 48px;
height: 48px;
}
.recommend .prev {
position: absolute;
left: 0;
top: 346px;
background-image: url(images/prev.png);
}
.recommend .next {
position: absolute;
right: 0;
top: 346px;
background-image: url(images/next.png);
}
.recommend .prev:hover {
background-image: url(images/prev_active.png);
}
.recommend .next:hover {
background-image: url(images/next_active.png);
}
</style>
</head>
<body>
<section class="recommend">
<div class="center-wrap">
<div class="similar">
<h3>精品推荐</h3>
<span>Boutique recommendation</span>
</div>
<a href="javascript:;" class='arrow prev' id='leftbtn'></a>
<a href="javascript:;" class='arrow next' id='rightbtn'></a>
<div class="carousel">
<ul class='list' id='list'>
<li>
<a href="#">
<img src="images/recommend1.png" alt="">
<div class="recomWords">轻奢风格样板房客厅色彩搭配装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend2.png" alt="">
<div class="recomWords">简约美式风格卧室衣柜设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend3.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend4.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend5.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend6.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend7.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend8.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
<li>
<a href="#">
<img src="images/recommend9.png" alt="">
<div class="recomWords">轻奢风格L型厨房装修设计</div>
</a>
</li>
</ul>
</div>
</div>
</section>
<script>
// 先得到元素
var list = document.getElementById('list');
var leftbtn = document.getElementById('leftbtn');
var rightbtn = document.getElementById('rightbtn');
// // 克隆
var clone_li = list.firstElementChild.cloneNode(true);
list.appendChild('clone_li');
var idx = 0;
var lock = true;
rightbtn.onclick = function () {
if (!lock) return;
lock = false;
list.style.transition = 'left .5s ease 0s';
idx++;
if (idx > 8) {
setTimeout(function () {
list.style.transition = 'none';
list.style.left = 0;
idx = 0;
}, 500)
}
list.style.left = -idx * 345 + 'px';
// 函数节流
setTimeout(function () {
lock = true;
}, 500);
}
leftbtn.onclick = function () {
if (!lock) return;
lock = false;
if (idx == 0) {
list.style.transition = 'none';
list.style.left = -9 * 345 + 'px';
setTimeout(function () {
list.style.transition = 'left .5s ease 0s';
idx = 8;
list.style.left = -idx * 345 + 'px';
}, 0)
} else {
idx--;
list.style.left = idx * 345 + 'px';
}
// 函数节流
setTimeout(function () {
lock = true;
}, 500);
}
</script>
</body>
</html>
1回答
同学你好,本作业中的轮播图,不需要做成无缝滚动的形式,它的可视区是展示3个图片,而无缝滚动只展示一个,二者有差别:
该轮播图效果如下:
以点击右侧按钮为例,每点击一次,轮播图往左侧切换一张,直到最后一张图显示到可视区之后,再点击右侧按钮,轮播图不会再往左侧切换,也不会回到第一张。
整体实现,可修改如下:
为了方便计算,将样式调整如下:
js调整如下:
同学按照上面修改一下,如果还有问题,可以提交作业,批复作业的老师,会针对代码,再次为同学修改优化的。
祝学习愉快!
相似问题
回答 1
回答 2