Javascript vuejs在滚动中修复侧栏, 数据(){ 返回{ 订单:[], 加载:false, fixedOrderPanel:错误 } }, 挂载(){ 这个 if(window!==未定义&&window.addEventListener){ window.addEventListener('scroll',()=>this.handleScroll(window.scrollY)); } }, 已销毁:函数(){//不工作 console.log('Afore') if(window!==未定义&&window.removeEventListener){ window.removeEventListener('scroll', ()=>this.handleScroll(window.scrollY)); } }, 方法:{ handleScroll:函数(滚动){ console.log('滚动') 如果(滚动>160){ this.fixedOrderPanel=true }否则{ this.fixedOrderPanel=false } }, }
Javascript vuejs在滚动中修复侧栏, 数据(){ 返回{ 订单:[], 加载:false, fixedOrderPanel:错误 } }, 挂载(){ 这个 if(window!==未定义&&window.addEventListener){ window.addEventListener('scroll',()=>this.handleScroll(window.scrollY)); } }, 已销毁:函数(){//不工作 console.log('Afore') if(window!==未定义&&window.removeEventListener){ window.removeEventListener('scroll', ()=>this.handleScroll(window.scrollY)); } }, 方法:{ handleScroll:函数(滚动){ console.log('滚动') 如果(滚动>160){ this.fixedOrderPanel=true }否则{ this.fixedOrderPanel=false } }, },javascript,vue.js,vuejs2,dom-events,vue-router,Javascript,Vue.js,Vuejs2,Dom Events,Vue Router,窗口。删除销毁/组件或路由更改VUEJ后仍保持完整的滚动侦听器的ventListenerwindow.removeEventListener每当更改路由或组件时都不工作,即使我尝试了PreDestroy()之前的和destroyed方法来删除滚动事件侦听器。 <div class="col-sm-4 my-orders-order-now"> <div :class="{ fixed: fixedOrderPanel }">
窗口。删除销毁/组件或路由更改VUEJ后仍保持完整的滚动侦听器的ventListener
window.removeEventListener
每当更改路由或组件时都不工作,即使我尝试了PreDestroy()之前的和destroyed方法来删除滚动事件侦听器。
<div class="col-sm-4 my-orders-order-now">
<div :class="{ fixed: fixedOrderPanel }">
<FixedPannel />
</div>
</div>
data () {
return {
orders: [],
loading: false,
fixedOrderPanel: false
}
},
mounted () {
this.getJokes()
if (window !== undefined && window.addEventListener) {
window.addEventListener('scroll',() => this.handleScroll(window.scrollY));
}
},
destroyed: function () { //Not working
console.log('Afore')
if (window !== undefined && window.removeEventListener) {
window.removeEventListener('scroll',
()=>this.handleScroll(window.scrollY));
}
},
methods: {
handleScroll: function(scrolled){
console.log('scrolling')
if (scrolled > 160) {
this.fixedOrderPanel = true
} else {
this.fixedOrderPanel = false
}
},
}
组成部分:{
固定板
},
挂载(){
这个
document.addEventListener('scroll',this.handleScroll);
},
已销毁:函数(){
document.removeEventListener('scroll',this.handleScroll);
},
方法:{
handleScroll:function(){
const checkWindow=window!==undefined&&window.scrollY;
如果(选中window&&window.scrollY>160){
this.fixedOrderPanel=true
}否则{
this.fixedOrderPanel=false
}
常量scrollFix=(已滚动)=>{
如果(滚动>160){
this.fixedOrderPanel=true
}否则{
this.fixedOrderPanel=false
}
}
}
}
.固定{
位置:固定;
顶部:0px;
填充:1%;
}
此方法的可能重复与上次发布此方法时的规范中列出的方法相同。我正在为侧边栏问题提供解决方案,如果没有匿名函数问题,请删除重复
<div class="col-sm-4 my-orders-order-now">
<div :class="{ fixed: fixedOrderPanel }">
<FixedPannel />
</div>
</div>
components: {
FixedPannel
},
mounted () {
this.getJokes()
document.addEventListener('scroll', this.handleScroll);
},
destroyed: function () {
document.removeEventListener('scroll', this.handleScroll);
},
methods: {
handleScroll: function(){
const checkWindow = window !== undefined && window.scrollY;
if (checkWindow && window.scrollY > 160) {
this.fixedOrderPanel = true
} else {
this.fixedOrderPanel = false
}
const scrollFix = (scrolled) => {
if (scrolled > 160) {
this.fixedOrderPanel = true
} else {
this.fixedOrderPanel = false
}
}
}
}
<style>
.fixed{
position: fixed;
top: 0px;
padding: 1%;
}
</style>