应用场景:换一批功能
<div class="search" onclick="handleClickChange()">换一批</div>
<ul></ul>
<script>
let textAllData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let page = 0
function handleClickChange () {
let res = changeFun(textAllData, 4)
let str = ''
res.forEach(item => {
str += `
<li>${item}</li>
`
})
document.querySelector('ul').innerHTML = str
}
function changeFun (allData, n) {
let length = allData.length
let result = length - page
let newArr = []
if (result > 0 && result < n) {
newArr = [...allData.slice(length - result, length), ...allData.slice(0, n - result)]
page = n - result
} else if (result >= n) {
newArr = allData.slice(page, page + n)
page += n
} else {
page = 0
newArr = allData.slice(page, page + n)
}
return newArr
}
</script>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。