简体   繁体   English

在 Vue 中滚动 100px 后显示 div 标签

[英]Show div tag after scrolling 100px from in Vue

I am working filename.vue file.我正在工作 filename.vue 文件。 I am trying to add a div to show up after scroll 100px.我正在尝试添加一个 div 以在滚动 100px 后显示。 I am not able to get it.我无法得到它。 Please help me find out where I am going wrong.请帮我找出我哪里出错了。

<template>
 <div id="sectionTop"> 
  <div id="scrollButton"> 
   <a class="topButton" href="javascript:document.getElementById('sectionTop').scrollIntoView(true);"><img src="../../images/uparrow.svg"></a>
  </div>
 </div>
</template>

<script>
 $(document).scroll(function() {
    var y = $(this).scrollTop();
    if (y > 800) {
      $('.scrollButton').fadeIn();
    } else {
      $('.scrollButton').fadeOut();
    }
  });
</script>

Updated as per suggested根据建议更新

<template>
 <div id="sectionTop"> 
  <div id="scrollButton"> 
   <a class="topButton" href="javascript:document.getElementById('sectionTop').scrollIntoView(true);" v-if="scrollpx > 800"><img src="../../images/uparrow.svg"></a>
  </div>
 </div>
</template>

<script>
  export default {    
    data() {
      return {
        scrollpx: 0
      };
    },
    methods: {
      handleScroll() {
        this.scrollpx = window.scrollY;
      }
    },
    created() {
      window.addEventListener('scroll', this.handleScroll);
    },
    destroyed() {
      window.removeEventListener('scroll', this.handleScroll);
    }
  };
</script>

You don't need jquery to do this.您不需要 jquery 来执行此操作。 You can register an event listener for scroll events in the created method of your vue component (don't forget to unregister it afterwards!).您可以在 vue 组件的created方法中为滚动事件注册一个事件侦听器(之后不要忘记取消注册!)。 In you handleScroll method you can then update a variable holding the current scrollY-value.handleScroll方法中,您可以更新保存当前 scrollY 值的变量。 Now this value is available to vue and can be used "reactively".现在这个值可用于 vue 并且可以“被动地”使用。 For example, you can show your button using v-if :例如,您可以使用v-if显示您的按钮:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="replace"></div> <script> new Vue({ el: '#replace', template: ` <div id="scroller"> <div id="sectionTop" style="height: 5000px"> Please Scroll down <div id="scrollButton"> <a class="topButton" style="position:fixed" v-if="scrollpx > 800">Scroll top</a> </div> </div> </div>`, data() { return { scrollpx: 0 }; }, methods: { handleScroll() { this.scrollpx = window.scrollY; } }, created() { window.addEventListener('scroll', this.handleScroll); }, destroyed() { window.removeEventListener('scroll', this.handleScroll); } }) </script>

This is how I solved this problem.这就是我解决这个问题的方法。

<a class="scrollTop" href="javascript:document.getElementById('sectionTop').scrollIntoView(true);" title='Click here to go to the top' v-if="scrollpx > 100"><img src="../../images/uparrow.svg"></a>

<script>
  export default {    
    data() {
      return {
        scrollpx: 0
      };
    },
    methods: {
      handleScroll() {
        this.scrollpx = document.body.scrollTop;
      }
    },
    mounted() {
      document.body.addEventListener('scroll', this.handleScroll);
    } 
  };

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM