原生JS:滚动屏案例
- 用ES6方式 实现一个自动滚屏的简单应用
- 为了阅读方便 js部分有些工具函数没有进行抽离
第一部分
index.html
<div class='wrap'>
<div class='list-hd'>
<h2> 书籍列表 </h2>
</div>
<div class='container'>
<ul class='bk-list'>
<li class='list-item'>
<a href="./detail.html" class="item-lk">
<div class="bk-pic">
<img src="./img/前1.jpg" alt="" />
</div>
<div class="bk-info">
<h2>完美世界</h2>
<p class="author">作者: 他乡的灯火</p>
<p class="intro">
李照冷酷无情,杀人如麻浑身散发着戾气,一个眼神就能让小儿夜啼不止。人人都道定国公府那位娇弱美人,嫁入将军府后撑不了一个月就要吓破了胆……
掉马小剧场 婚后,李照大气不敢喘,眉头不敢皱,生怕吓坏了美人。
直到这一日,李照得知穆清瑜去了贤王妃设的鸿门宴,手提长剑凶神恶煞赶过去。
他的夫人纯良柔弱,进了贤王府那个狼窝岂不是要被啃得连渣都不剩?
赶到贤王府,只见贤王妃的寝殿冒着窜天的火光,王妃被火熏得狼狈不堪,痛骂着毫发无损的穆清瑜。
穆清瑜柔柔弱弱陪着不是,暗中却把手里的火折子丢了出去,瞬间偏殿也起了火。
见到李照,穆清瑜扑到其怀里小声啜泣:“着火了我好害怕~”
目睹一切的李照:“……”
</p>
<p>更新时间: 2022-03-03</p>
</div>
</a>
</li>
</ul>
</div>
<a href="javascript:;" class="s-stop-btn"> </a>
</div>
<script src='./js/index.js'></script>
CSS-初始化样式
body{
margin: 0;
}
div {
box-sizing: border-box;
}
ul{
list-style: none;
margin: 0;
padding: 0;
}
a{
text-decoration:none;
}
h1,h2,h3,h4,p{
margin: 0;
font-weight: normal;
}
img{
height: 100%;
width: 100%;
}
.list-hd{
position: fixed;
top:0;
left: 0;
width:100%;
height: 44px;
line-height: 44px;
text-align: center;
color: #fff;
z-index: 2;
background-color: orange;
}
.container {
margin-top: 44px;
}
common.css
.bk-list .list-item{
position: relative;
height: 150px;
border-bottom: 1px solid #000;
}
.bk-list .list-item .item-lk{
display: block;
height: 100%;
}
.bk-list .list-item .item-lk .bk-pic{
position: absolute;
top: 0;
left: 0;
height: 150px;
padding: 10px;
}
.bk-list .list-item .item-lk .bk-info{
height: 100%;
margin-left: 118px;
color: #424242;
}
.list-item .item-lk .bk-info .author{
margin: 10px 0 10px 0;
}
.list-item .item-lk .bk-info .intro{
font-size: 12px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: red;
margin-bottom: 10px;
}
.s-stop-btn{
display: none;
position: fixed;
bottom: 25px;
right: 15px;
width: 60px;
height: 60px;
border-radius: 50%;
background: url(../img/rocket.png) no-repeat 0 0/100% 100%;
opacity: .7;
}
- 图片 rocket.png

index.js
let scrollBtn = (function() {
class Per {
constructor(options) {
this.sTopBtn = options.sTopBtn
}
addEvent(el, type, fn, bl) {
if(el.addEventListener) {
el.addEventListener(type, fn, bl)
}else {
el['on' + type] = fn;
}
}
getScrollOffset() {
return {
left: window.scrollX || document.documentElement.scrollLeft,
top: window.scrollY || document.documentElement.scrollTop
}
}
showBtn(){
let sTop = this.getScrollOffset().top;
sTop ? (this.sTopBtn.style.display = 'block')
: (this.sTopBtn.style.display = 'none')
}
init() {
this.addEvent(window, 'scroll', this.showBtn.bind(this), false);
this.addEvent(this.sTopBtn, 'click', function() {
window.scrollTo(0, 0);
}, false)
}
}
return Per;
})();
new scrollBtn({
sTopBtn: document.querySelector('.s-stop-btn')
}).init();
第二部分
detail.html
<div class="list-hd">
<h2>小说详情</h2>
</div>
<div class='container'>
</div>
<a href="javascript:;" class="round s-stop-btn"></a>
<a href="javascript:;" class="round play-btn"></a>
<script src="./js/delte.js"></script>
detail.css
.container {
margin-top: 20px;
padding: 10px;
}
.round {
display: block;
position: fixed;
right: 25px;
width: 60px;
height: 60px;
border-radius: 50%;
background-color: #000;
}
.round:hover {
opacity: 1;
}
.s-stop-btn {
display: none;
bottom: 100px;
background: url(../img/rocket.png) no-repeat 0 0/100% 100%;
opacity: 0.7;
}
.play-btn {
bottom: 25px;
background: orange url(../img/play.png) no-repeat 12px 12px/60% 60%;
text-align: center;
line-height: 50px;
}


detail.js
let scrollBtn = (function () {
let playing = false,
t = null;
let toolFn = function () {
return {
scrollTop: window.scrollY || document.documentElement.scrollTop,
viewHeight: window.innerHeight || document.documentElement.clientHeight,
scrollHeight: document.body.scrollHeight || document.documentElement.scrollHeight,
};
};
class Per {
constructor(options) {
this.sTopBtn = options.sTopBtn;
this.playBtn = options.playBtn;
this.playImg = options.playImg;
this.pauseImg = options.pauseImg;
}
addEvent(el, type, fn, bl) {
el.addEventListener ? el.addEventListener(type, fn, bl) : (el["on" + type] = fn);
}
showBtn() {
toolFn().scrollTop ? (this.sTopBtn.style.display = "block")
: (this.sTopBtn.style.display = "none");
}
setPlay() {
let sTop = toolFn().scrollTop,
vHeight = toolFn().viewHeight,
dHeight = toolFn().scrollHeight,
_self = this;
if(dHeight === sTop + vHeight) {
return
}
if(!playing) {
t = setInterval(() => {
sTop = toolFn().scrollTop;
if(dHeight <= sTop + vHeight) {
clearInterval(t)
_self.playBtn.style.backgroundImage = 'url('+ _self.playImg +')'
playing = false
}else {
window.scrollBy(0, 1)
_self.playBtn.style.backgroundImage = 'url(' + _self.pauseImg + ')';
}
}, 20)
playing = true
}else {
clearInterval(t);
_self.playBtn.style.backgroundImage = 'url(' + _self.playImg + ')';
playing = false
}
}
init() {
let _self = this;
this.addEvent(window, "scroll", this.showBtn.bind(this), false);
this.addEvent(this.sTopBtn, 'click', function() {
window.scrollTo(0, 0)
clearInterval(t);
playing = false
_self.playBtn.style.backgroundImage = 'url(' + _self.playImg + ')';
},false)
this.addEvent(this.playBtn, 'click', this.setPlay.bind(this), false)
}
}
return Per;
})();
new scrollBtn({
sTopBtn: document.querySelector(".s-stop-btn"),
playBtn: document.querySelector(".play-btn"),
playImg: "./img/play.png",
pauseImg: "./img/pause.png",
}).init();