自己做了个练习题,怎么用JS实现自动滚动功能,没有任何思路麻烦老师帮忙看看,谢谢
如果滚动条到达最右边 重复回到最左边滚动。。
滚动条左右可以滑动,找到对应的图片广告。。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>自动滚动</title>
<link rel="stylesheet" href="style5.css">
</head>
<body>
<div class="jd_container nice-goods" aria-label="发现好货">
<a href="#" class="nice-goods_logo nice-goods_logo-bg-1">
<div class="logo-container">
<div class="logo-text">
探索新生活
<i class="jiantou01"></i>
</div>
</div>
</a>
<div class="nice-goods_recommends" id="automatic">
<div class="goods-list" id="con-ul" style="position: relative;left: 0;">
<a href="#" class="goods goods--bottom">
<div class="goods_name">黑街多功能变形轮滑鞋</div>
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/5a72cf86Nc0bdfcb3.jpg!q70.jpg" alt="黑街多功能变形轮滑鞋" class="lazyimg_img">
</div>
</div>
</a>
<a href="#" class="goods goods--top">
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/05088cf4612dc99c.jpg" alt="MAC 圣诞限量 口红套装" class="lazyimg_img">
</div>
</div>
<div class="goods_name">MAC 圣诞限量 口红套装</div>
</a>
<a href="#" class="goods goods--bottom">
<div class="goods_name">环境猫 小巧便携 检测仪</div>
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/9ec8d31355c44409.png" alt="环境猫 小巧便携 检测仪" class="lazyimg_img">
</div>
</div>
</a>
<a href="#" class="goods goods--top">
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/5a716ad2N3afe0948.png" alt="金士顿高稳定性U盘" class="lazyimg_img">
</div>
</div>
<div class="goods_name">金士顿高稳定性U盘</div>
</a>
<a href="#" class="goods goods--bottom">
<div class="goods_name">小米智能机顶盒</div>
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/7834c3a9761f8385.jpg!q70.jpg" alt="小米智能机顶盒"class="lazyimg_img">
</div>
</div>
</a>
<a href="#" class="goods goods--top">
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/1e6cde18031528db.jpg" alt="公牛 智能 插座" class="lazyimg_img">
</div>
</div>
<div class="goods_name">公牛 智能 插座</div>
</a>
<a href="#" class="goods goods--bottom">
<div class="goods_name">IDX高帮火烈鸟板鞋</div>
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/5aaa1734Ndfebe256.jpg!q70.jpg" alt="IDX高帮火烈鸟板鞋" class="lazyimg_img">
</div>
</div>
</a>
<a href="#" class="goods goods--top">
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/1f9dcdbc6bd3a6d7.jpg" alt="史丹利 不锈钢 保温马克杯" class="lazyimg_img">
</div>
</div>
<div class="goods_name">史丹利 不锈钢 保温马克杯</div>
</a>
<a href="#" class="goods goods--bottom">
<div class="goods_name">3M呼气阀防雾霾口罩</div>
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/5a71b624N2cc652fb.jpg!q70.jpg" alt="3M呼气阀防雾霾口罩" class="lazyimg_img">
</div>
</div>
</a>
<a href="#" class="goods goods--top">
<div class="goods_image">
<div class="lazyimg lazyimg_loaded">
<img src="img/5a71cf79Ne9407815.jpg!q70.jpg" alt="Justice 板面" class="lazyimg_img">
</div>
</div>
<div class="goods_name">Justice 板面</div>
</a>
</div>
<div class="scroll-bar">
<span class="scroll-points" id="out" style="transform: translate3d(787.987px,0px,0px);width: 99px;"></span>
</div>
</div>
</div>
<!--<div id="div1">
<ul>
<li><img src="img/5a5082c8Nd0b374ac.jpg" alt=""></li>
<li><img src="img/5b5ae2f6N6b00abe4.jpg!q70.jpg" alt=""></li>
<li><img src="img/58fc8adfNdbc30fa2.jpg!q70.jpg" alt=""></li>
<li><img src="img/59b9d9b2Nbec847a8.jpg!q70.jpg" alt=""></li>
</ul>
</div>-->
<script src="script5.js"></script>
</body>
</html>
*{
margin: 0;
padding: 0;
}
/*#div1{
width: 712px;
height: 108px;
margin: 100px auto;
position: relative;
overflow: hidden;
}
#div1 ul{
position:absolute;
left: 0;
top: 0;
}z
#div1 ul li {
float: left;
width: 178px;
height: 108px;
list-style: none;
margin-left: 10px;
}*/
a{
color: #666;
text-decoration: none;
}
a:hover {
color: #c81623;
}
.jd_container{
width: 1190px;
margin-left: auto;
margin-right: auto;
}
.nice-goods{
height: 260px;
position: relative;
box-sizing: border-box;
padding-left: 190px;
margin-bottom: 20px;
}
.nice-goods_logo-bg-1{
background-image: url("img/disk.png");
}
.nice-goods_logo{
background-color: #eee;
background-size: 100% 100%;
position: absolute;
left: 0;
top: 0;
width: 190px;
height: 100%;
}
.nice-goods_logo .logo-container{
width: 160px;
height: 90px;
margin-top: 15px;
margin-left: auto;
margin-right: auto;
background-image: url("img/Find-good-goods.png");
background-size: contain;
background-position: 50%;
background-repeat: no-repeat;
}
.logo-text{
color: rgba(255,255,255,.8);
font-size: 16px;
padding-top: 55px;
padding-left: 20px;
padding-bottom: 10px;
display: block;
}
.logo-text i{
background-image: url("img/sprite.png");
background-position: 0 -16px;
width: 16px;
height: 16px;
position: relative;
margin-left: 2px;
margin-top: -3px;
vertical-align: middle;
display: inline-block;
}
.nice-goods_logo:hover .jiantou01 {
background: url(img/sprite.png);
background-position: 0 0;
transition: background-position 0.5ms ease-in-out;
}
.nice-goods_recommends{
background-color: white;
overflow: hidden;
}
.nice-goods_recommends{
margin-left: 10px;
height: 100%;
position: relative;
}
.nice-goods_recommends .goods-list{
padding-left: 20px;
padding-right: 20px;
height: 100%;
width: 100%;
box-sizing: border-box;
white-space: nowrap;
}
.nice-goods_recommends .goods-list .goods--bottom{
margin-top: 50px;
}
.nice-goods_recommends .goods-list .goods{
width: 150px;
display: inline-block;
margin-right: 50px;
vertical-align: top;
}
.goods--bottom .goods_name{
margin-bottom: 10px;
}
.nice-goods_recommends .goods-list .goods_name{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: #333;
transition: color .2s ease;
text-align: center;
font-size: 14px;
}
.goods-list .goods_image{
transition: opacity .2s ease;
}
.nice-goods_recommends .goods-list .goods_image{
height: 150px;
}
.lazyimg_loaded{
transition: background .2s linear,opacity .2s linear!important;
background: transparent;
}
.lazyimg{
position: relative;
overflow: hidden;
}
.goods_image .lazyimg_img{
width: 100%;
height: 100%;
}
.lazyimg_loaded .lazyimg_img{
opacity: 1;
}
.nice-goods_recommends .goods-list .goods--top{
margin-top: 20px;
}
.nice-goods_recommends .goods-list .goods--top .goods_name{
margin-top: 10px;
}
.nice-goods_recommends .scroll-bar{
position: absolute;
left: 15px;
bottom: 10px;
width: calc(100% - 30px);
height: 3px;
border-radius: 3px;
background-color: #f3f3f3;
opacity: 0;
transition: opacity .3s ease;
visibility: hidden;
}
.nice-goods_recommends .scroll-bar .scroll-points{
width: 33px;
height: 9px;
border-radius: 4px;
background-color: #d8d8d8;
position: absolute;
top: 50%;
margin-top: -4.5px;
}
.nice-goods_recommends:hover .scroll-bar{
opacity: 1;
visibility: visible;
}
window.onload = init;
let conUl = document.getElementById('con-ul');
let timer = null;
function init() {
timer = setInterval('common()', 20);
}
// 把定时器函数封装
function common() {
let left = parseInt(conUl.style.left);
conUl.style.left = (left - 1) + 'px';/**/
// 判断left距离是否到了ul宽度的三分之二,如果是就把left设置为零
if (Math.abs(parseInt(conUl.style.left)) === parseInt(conUl.offsetWidth * 2 / 3)) {
conUl.style.left = 0;
}
}
// 鼠标移入清除定时器
conUl.onmousemove = function() {
clearInterval(timer);
}
// 鼠标移除重新设置定时器/**/
conUl.onmouseout = function() {
timer = setInterval('common()', 20);
}
aobj=document.getElementById('out');
document.οnmοusemοve=function(event){
x=event.clientX-100;
y=event.clientY-50;//监听鼠标位置;
document.title=x+'-'+y;
aobj.style.left=x+'px';
aobj.style.top=y+'px';
}
正在回答 回答被采纳积分+1
兄弟,我只能实现第一步,给你看看
高度盖住了滚动条,你设小一点,还有宽度应该是小盒子的宽度啊。
function common() { let left = parseInt(conUl.style.left); conUl.style.left = (left - 1) + 'px'; /**/ var res = (Math.abs(parseInt(conUl.style.left)) / (goodsList.offsetWidth - ngr.offsetWidth)) * (scrollBar.offsetWidth - scrollPoints.offsetWidth); scrollPoints.style.left = res + 'px'; // 判断left距离是否到了ul宽度的三分之二,如果是就把left设置为零 // if (Math.abs(parseInt(conUl.style.left)) === parseInt(conUl.offsetWidth * 2 / 3)) { if (Math.abs(parseInt(conUl.style.left)) === (goodsList.offsetWidth - ngr.offsetWidth)) { conUl.style.left = 0; } }
我用的是比例,内容的left/显示内容的宽度(2000-990)*(线长-滚动条宽度)。还有下面判断条件我也改了,内容全部显示出来的条件是(2000-990) 。
你说的拖动滚动条这个效果,得需要H5拖拽,还是用插件吧,原生太麻烦了,要不就别考虑这个了。
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星