自动滑动vue版本
<template>
<div class="revenueSort-content">
<div class="revenueSort-scroll" id="revenueSortCon" @mouseover="mouseOverHandle" @mouseleave="mouseLeaveHandle">
<div class="revenueSort-box">
<div class="sort-item" v-for="(item,index) in revenueSortList" :key="index" >
<div class="sort-tit">
<span>{{item.qymc}}</span>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
revenueSortList: [],
heightNum: "",
top: 0,
timeInit: "",
};
},
created() {
// 获取数据
this.getRevenueList()
},
methods: {
//鼠标移入事件
mouseOverHandle() {
let top = this.top
let heightNum = this.revenueSortList.length / 2
let height = this.heightNum
var boxHeight = document.querySelector("#revenueSortCon").offsetHeight
let num = height * heightNum - boxHeight
var minHeight = 0
var maxHeight = 0
if (num >= top) {
minHeight = 0
maxHeight = num
} else {
minHeight = (this.revenueSortList.length / 2) * height
maxHeight = (this.revenueSortList.length) * height - boxHeight
}
document.getElementById("revenueSortCon").onmousewheel = (e) => {
if (e.wheelDeltaY < 0) {
this.top += 40;
} else {
this.top -= 40;
}
if (this.top < minHeight) {
this.top = minHeight;
}
if (this.top > maxHeight) {
this.top = maxHeight;
}
document.getElementById('revenueSortCon').scrollTop = this.top
return false;
};
this.stopTimeFun()
},
// 鼠标移出事件
mouseLeaveHandle() {
let top = document.getElementById('revenueSortCon').scrollTop
let num = (this.revenueSortList.length / 2) * this.heightNum
if (num < top) {
this.top = top - num
}
this.initTimeFun()
},
//开始滚动执行
initTimeFun() {
if (this.timeInit) {
this.stopTimeFun()
}
let heightNum = this.revenueSortList.length / 2
let height = this.heightNum
let num1 = height * heightNum
this.timeInit = window.setInterval(() => {
if (this.top == num1) {
this.top = 0
}
this.top = this.top + 1
document.getElementById('revenueSortCon').scrollTop = this.top
}, 40)
},
// 清楚定时器
stopTimeFun() {
let timeInit = this.timeInit
window.clearInterval(timeInit);
},
// 获取营收数据
getRevenueList() {
this.$api.indexApi.getRevenueListSort(this.dataObj).then(({data} = res) => {
if (data.success) {
let list = data.result
this.stopTimeFun()
if (list.length > 0) {
document.getElementById('revenueSortCon').scrollTop = 0
this.top = 0
let firstNum = list[0].zysr
list.forEach((item, index) => {
let percent = parseFloat(((100 * item.zysr) / firstNum).toFixed(2))
item["percent"] = percent + "%"
item["index"] = index
})
this.revenueSortList = list
this.$nextTick(() => {
this.heightNum = document.querySelector(".sort-item").offsetHeight
let boxHeight = document.querySelector(".revenueSort-content").offsetHeight
if (this.heightNum * (list.length) > boxHeight) {
//滚动插件 复制2分数据
let list = [...this.revenueSortList]
this.revenueSortList = this.revenueSortList.concat(list)
setTimeout(() => {
this.initTimeFun()
}, 1000)
}
});
}else{
this.$message({
message: '暂无数据!',
type: 'warning'
});
this.revenueSortList = []
}
} else {
this.$message({
message: '暂无数据!',
type: 'warning'
});
this.revenueSortList = []
}
})
},
},
mounted() {
},
beforeDestroy() {
this.stopTimeFun()
}
};
</script>
<style lang="scss">
.revenueSort-content {
height: 25vh;
background: url("../../assets/imgs/index/model02.png") no-repeat top center;
background-size: 100% 100%;
margin-top: 0.4vh;
overflow: hidden;
display: flex;
flex-direction: column;
.model-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 14px 12px 8px;
cursor: pointer;
i {
color: #00f6ff;
font-size: 22px;
vertical-align: middle;
}
span {
color: #00f6ff;
font-size: 16px;
vertical-align: middle;
padding-left: 8px;
}
.unit {
color: #b2b8ca;
}
}
.sort-item {
height: 42px !important;
cursor: pointer;
}
.tempWrap {
padding: 14px;
padding-top: 0;
padding-bottom: 0;
}
.revenueSort-scroll {
margin-bottom: 20px;
flex: 1;
overflow: scroll;
position: relative;
overflow-x: hidden;
/*滚动条样式*/
&::-webkit-scrollbar {
width: 10px;
}
&::-webkit-scrollbar-thumb {
border-radius: 2px;
/*background-color: red;*/
background: rgba(255, 255, 255, 0.2);
}
}
.revenueSort-box {
padding: 10px;
padding-top: 0;
width: 100%;
box-sizing: border-box;
.sort-item {
padding-bottom: 8px;
.sort-tit {
display: flex;
align-items: center;
color: #fff;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
.img-box {
text-align: center;
color: red;
width: 30px;
.icon1 {
width: 25px;
height: 23px;
margin: 0 auto;
background: url("../../assets/imgs/index/icon1.png") no-repeat center center;
}
.icon2 {
margin: 0 auto;
width: 16px;
height: 23px;
background: url("../../assets/imgs/index/icon2.png") no-repeat center center;
background-size: 100% 100%;
}
}
span {
margin-left: 4px;
font-size: 14px;
}
}
.sort-bar {
display: flex;
align-items: center;
color: #f1c01d;
padding-top: 2px;
.bar {
max-width: 296px;
height: 10px;
margin-left: 2px;
background-image: linear-gradient(270deg,
#ded123 0%,
#f1c01d 100%);
border-radius: 7px;
margin-right: 8px;
}
}
}
}
}
</style>
鼠标不无限滚动
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
li {
list-style-type: none;
background-color: rgba(0, 0, 0, 0.2);
line-height: 40px;
color: #000;
}
#map {
background-color: pink;
overflow: hidden;
position: relative;
}
.li-box {
position: relative;
top: 0;
left: 0;
width: 100;
}
</style>
</head>
<body>
<div id="map" style="width: 500px; height: 200px">
<div class="li-box" id="scrollBox">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</div>
</div>
<script src="./js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$("#map").hover(
function () {
// 移入
var firstTop = $("#scrollBox").css("top"); //已经滚动的高度
var i = 0;
var minTop = 0; //允许的最小top
var maxTop = oneHeight; //允许的最大top
if (firstTop != "0px") {
firstTop = parseFloat(firstTop);
i = Math.floor(firstTop / height); //已经滚动上去了几个
minTop = i * height;
maxTop = oneHeight + -minTop;
} else {
firstTop = 0;
}
maxTop = maxTop - $("#map").height();
document.querySelector("#map").onmousewheel = function (e) {
if (e.wheelDeltaY < 0) {
scrollTop -= 40;
} else {
scrollTop += 40;
}
if (scrollTop > minTop) {
scrollTop = minTop;
}
if (scrollTop < -maxTop) {
scrollTop = -maxTop;
}
$("#scrollBox").css("top", scrollTop);
};
stopTimeFun();
},
function () {
// 移出
var firstTop = $("#scrollBox").css("top"); //已经滚动的高度
firstTop = parseFloat(firstTop).toString().slice(1);
if (firstTop > oneHeight) {
scrollTop = -(firstTop - oneHeight);
}
initTimeFun();
}
);
// 复制两份数据
var cloneDom = $("#map li").clone();
$("#map .li-box").append(cloneDom);
//开始滚动执行
var timeInit = "",
scrollTop = 0;
var height = $("#map li").height(); //一行的高度
var liLength = $("#map li").length; //克隆之后总个数,总行数
var oneHeight = (liLength / 2) * height; //一份的高度
initTimeFun();
function initTimeFun() {
if (timeInit) {
stopTimeFun();
}
let heightNum = liLength / 2;
let num1 = height * heightNum;
timeInit = window.setInterval(() => {
if (scrollTop == -num1) {
scrollTop = 0;
}
scrollTop = scrollTop - 1;
$("#scrollBox").css("top", scrollTop);
}, 40);
}
// 清楚定时器
function stopTimeFun() {
window.clearInterval(timeInit);
}
</script>
</body>
</html>
鼠标无限滚动
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
li {
list-style-type: none;
background-color: rgba(0, 0, 0, 0.2);
line-height: 40px;
color: #000;
}
#map {
background-color: pink;
overflow: hidden;
position: relative;
}
.li-box {
position: relative;
top: 0;
left: 0;
width: 100;
}
</style>
</head>
<body>
<div id="map" style="width: 500px; height: 200px">
<div class="li-box" id="scrollBox">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</div>
</div>
<script src="./js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$("#map").hover(
function () {
// 移入
document.querySelector("#map").onmousewheel = function (e) {
if (e.wheelDeltaY < 0) {
scrollTop -= 40;
} else {
scrollTop += 40;
}
if(scrollTop>0){
scrollTop = -oneHeight;
}
if(scrollTop<-oneHeight){
scrollTop=0
}
$("#scrollBox").css("top", scrollTop);
};
stopTimeFun();
},
function () {
// 移出
var firstTop = $("#scrollBox").css("top"); //已经滚动的高度
firstTop = parseFloat(firstTop).toString().slice(1);
if (firstTop > oneHeight) {
scrollTop = -(firstTop - oneHeight);
}
initTimeFun();
}
);
// 复制两份数据
var cloneDom = $("#map li").clone();
$("#map .li-box").append(cloneDom);
//开始滚动执行
var timeInit = "",
scrollTop = 0;
var height = $("#map li").height(); //一行的高度
var liLength = $("#map li").length; //克隆之后总个数,总行数
var oneHeight = (liLength / 2) * height; //一份的高度
initTimeFun();
function initTimeFun() {
if (timeInit) {
stopTimeFun();
}
let heightNum = liLength / 2;
let num1 = height * heightNum;
timeInit = window.setInterval(() => {
if (scrollTop == -num1) {
scrollTop = 0;
}
scrollTop = scrollTop - 1;
$("#scrollBox").css("top", scrollTop);
}, 40);
}
// 清楚定时器
function stopTimeFun() {
window.clearInterval(timeInit);
}
</script>
</body>
</html>