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

鼠标悬浮时鼠标滚动示例 #226

Open
yanxj0 opened this issue Jun 1, 2024 · 1 comment
Open

鼠标悬浮时鼠标滚动示例 #226

yanxj0 opened this issue Jun 1, 2024 · 1 comment

Comments

@yanxj0
Copy link

yanxj0 commented Jun 1, 2024

<vue-seamless-scroll ref="scrollRef" @mouseenter.native="handleMouseEnter" @mouseleave.native="handleMouseLeave">
   ...
</vue-seamless-scroll>
export default{
data(){
return {scrollTop:0}
},
methods:{
handleMouseEnter(){
  let wrap = this.$refs.scrollRef.$el.children[0];
  let rect = wrap.getBoundingClientRect();
  let half = rect.height/2;
  
  let matchs = wrap.style.transform.match(/,\s+(.+?)px\)$/);
  if(matchs.length){
    let translate = Math.abs(Number(matchs[1]));
    let index = +(translate > half));
    wrap.children[index].classList.add('scroll');
    wrap.children[index].style.cssText += `--y:${translate}px;`;
    wrap.children[+!index].classList.add('ul-hidden');
    this.$nextTick(()=>{
      wrap.children[index].scrollTo({
      top:translate % rect.height,
      behavior: "instant"});
    });
    wrap.children[index].addEventistener('scroll',this.handleScroll.bind(this));
  }
},
handleScroll(evt){this.scrollTop = evt.target.scrollTop;},
},
handleMouseLeave(){
  let wrap = this.$refs.scrollRef.$el.children[0];
  for(let i=0;i<2;i++){
    wrap.children[i].classList.remove('scroll');
    wrap.children[i].classList.remove('ul-hidden');
    wrap.children[i].style.cssText = 'overflow:hidden;';
    wrap.children[i].removeEventListener('scroll',this.handleScroll.bind(this));
  }
  this.$refs.scrollREf.yPos = -this.scrollTop;
}
.scroll{
  over-flow-y: auto !important;
  tansform:translateY(var(--y));
  height: 270px;
  // 隐藏滚动条
  width:calc(100% - 8px);
}
.ul-hidden{
  visibility:hidden;
}
@qnone-x
Copy link

qnone-x commented Nov 7, 2024

let index = +(translate > half)); 多了)

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

No branches or pull requests

2 participants