在开发中常常会遇到这样一个vue页面,页面分为左右两部分,左边是目录树,右边是一个类名为xq-box的div,在xq-box中多个div上下并列布局,每个div中的内容就对应着左边目录树中的相应节点,现在的目标是,要监听这个xq-box滚动事件,右边一旦开始滚动,就要知道滚动到哪个子div,并让左边的目录树中对应的节点高亮显示.要怎么做呢? 1.首先,先写好大概的页面布局,这里要注意,右边xq-box的子div要绑定"'xqItem'+序号"的id,为了下面用js能获取到匹配的dom元…
在html中,写一个id为type的div: <div class="type" id="type"></div> css: .type{ height: 600px; overflow-y: auto; } 当里面的内容超过高度时,div会出现滚动条,监听这个div的滚动事件: //监听这个dom的scroll事件 document.getElementById("type").addEventListener(&quo…
vue 监听页面窗口大小 export default { name: 'Full', components: { Header, Siderbar }, data () { return { screenWidth: document.body.clientWidth, // 屏幕宽度 timer: false } }, computed: { isCollapse: { get () { return this.screenWidth < 768 }, set () { } } }, wat…
在项目中由于某些div元素在布局的时候需要初始化宽高,因为当浏览器缩小屏幕的时候需要重新刷新页面视图. 分析思路: 1.在store中创建state,用于保存当前浏览器的宽.高值. 2.在mounted中,使用window.onresize,监听页面大小是否发生变化.若发生变化则,则this.$store.commit修改store中的的宽.高: 3.在computed中获取到宽高的值.由于页面宽或者高其中一个变化都需要重新进行页面视图刷新,因此在computed中进行宽高合并,只需要监听合并后…
今天在项目开发中遇到一个根据数组中某个属性变化同时更新另一个属性变化的问题,刚开始代码如下 this.weekList1=r.data.roomProducts; this.weekList1.map(item=>{ item.cus_price=''; item.cus_plaPrice=''; item.cus_addPrice='';}); watch:{ weekList1:{ handler:function(newVal,oldval){ let self=this; newVal.…
  computed: {   isFollow () {     return this.$store.state.demo.id; //需要监听的数据   } }, watch: {   isFollow (newVal, oldVal) {     //do something   } },…
<script> var scrollFunc = function (e) { var direct = 0; e = e || window.event; if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件 if (e.wheelDelta > 0) { //当滑轮向上滚动时 alert("滑轮向上滚动"); } if (e.wheelDelta < 0) { //当滑轮向下滚动时 alert("滑轮向下滚动");…
一,在 created中 注册 页面刷新和关闭事件 created() {  window.addEventListener('beforeunload', e => this.test(e)) }   二,事件,将你的逻辑方法加进去 methods: {  test(e) {   console.log('刷新或关闭')   // ...  } } 三,卸载注册的事件 destroyed() {  window.removeEventListener('beforeunload', e =>…
①监听页面滚动 在生命周期mounted中进行监听滚动: mounted () { window.addEventListener('scroll', this.scrollToTop) }, 在方法中定义监听滚动执行的方法: scrollToTop() { var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; console.log(scrollT…
监听页面滚动 在methods中定义一个方法 handleScroll() { //获取滚动时的高度 let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; let oneHeight = this.$refs.scrollOne.offsetHeight ; let twoHeight = this.$refs.scrollTwo.offsetHei…