Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第288天 用js封装一个前端分页的库,说说你的思路 #1834

Open
haizhilin2013 opened this issue Jan 28, 2020 · 2 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第288天 用js封装一个前端分页的库,说说你的思路

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Jan 28, 2020
@poyoho
Copy link

poyoho commented Feb 8, 2021

使用hash路由实现分页
好处就是不用刷新页面,来刷新内容。

 <body>
  <a href="#pageA">pageA</a>
  <a href="#pageB">pageB</a>
  <div class="a">
    hello world
  </div>
  <div class="b">
    你好
  </div>
<script>
const pageA = document.querySelector('.a')
const pageB = document.querySelector('.b')
pageA.setAttribute('style', 'display: none')
pageB.setAttribute('style', 'display: none')

window.addEventListener('load', () => {
  const hash = window.location.hash.slice(1)
  if (!hash) {
    window.location.hash = '/'
  }
})

window.addEventListener('hashchange', () => {
  const hash = window.location.hash.slice(1)
  switch (hash) {
  case 'pageA':
    pageA.setAttribute('style', 'display: block')
    pageB.setAttribute('style', 'display: none')
    break
  case 'pageB':
    pageA.setAttribute('style', 'display: none')
    pageB.setAttribute('style', 'display: block')
    break
  default:
    pageA.setAttribute('style', 'display: none')
    pageB.setAttribute('style', 'display: block')
    break
  }
})
</script>

@xiaoqiangz
Copy link

const pagination = (function(param) {
function prev() {}
function next() {}
function first() {}
function last() {}
function jump() {}
return {
prev,
next,
first,
last,
jump
}
}(data))
pagination.prev()
pagination.next()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

3 participants