【发布时间】:2020-07-07 14:36:13
【问题描述】:
我在这里工作: http://nufaith.ca/justinatkins/index.html
随时查看我的源代码,下面的 app.js 代码
EDIT1:我在顶部隐藏了透明徽标,现在您可以清楚地看到 2 个不同的英雄图像
目标:按箭头键或 V 形图标,主英雄图像将发生变化 + 顶部图像的模糊副本
问题:当你@click 人字形时,它们不会像你按箭头键那样做同样的事情
编辑2: 这是我的整个 app.js
window.Event = new Vue()
Vue.use(VueLazyload)
Vue.component('contact', {
template: `
<div class="contact">
<a href="contact.html">CONTACT</a>
</div>
`,
})
Vue.component('hero-bg', {
template: `
<div class="hero-bg">
<div class="hero">
<img id="pushed" :src="selectedItem.img" />
</div>
</div>
`,
data() {
return {
gridEnabled: false,
selected: 0,
thing: true
}
},
computed: {
selectedItem() {
return info[this.selected]
},
maxImages() {
return info.length - 1
}
},
created() {
window.addEventListener('keydown', (e) => {
if (e.keyCode === 37) {
this.previous()
return
}
if (e.keyCode === 39) {
this.next()
return
}
})
Event.$on('updateImg', (index) => {
this.selected = index
this.gridEnabled = !this.gridEnabled
})
},
methods: {
next() {
this.selected === this.maxImages ? this.selected = 0 : this.selected += 1
},
previous() {
this.selected === 0 ? this.selected = this.maxImages : this.selected -= 1
}
}
})
Vue.component('hero', {
template: `
<div class="hero-container" v-if="!gridEnabled">
<div class="hero">
<img :src="selectedItem.img" v-if="thing" alt=""/>
</div>
<div class="hero-desc">
<button class="control left" @click="previous">
<i class="zmdi zmdi-chevron-left"></i>
</button>
<span class="hero-desc-title" v-html="title"></span>
<button class="control right" @click="next">
<i class="zmdi zmdi-chevron-right"></i>
</button>
<br/>
<button class="view-all-button" @click="enableGrid">OVERVIEW</button>
</div>
</div>
`,
data() {
return {
gridEnabled: false,
selected: 0,
thing: true
}
},
computed: {
selectedItem() {
return info[this.selected]
},
title() {
const comma = this.selectedItem.title.indexOf((','))
const len = this.selectedItem.title.length
const strBeginning = this.selectedItem.title.substring(comma, 0)
const strEnd = this.selectedItem.title.substring(comma, len)
if (this.selectedItem.title.includes(',')) {
return `<span>${strBeginning}<span class="font-regular font-muted">${strEnd}</span></span>`
}
return this.selectedItem.title
},
maxImages() {
return info.length - 1
}
},
created() {
window.addEventListener('keydown', (e) => {
if (e.keyCode === 37) {
this.previous()
return
}
if (e.keyCode === 39) {
this.next()
return
}
})
Event.$on('updateImg', (index) => {
this.selected = index
this.gridEnabled = !this.gridEnabled
})
},
methods: {
next() {
this.selected === this.maxImages ? this.selected = 0 : this.selected += 1
},
previous() {
this.selected === 0 ? this.selected = this.maxImages : this.selected -= 1
},
enableGrid() {
this.gridEnabled = !this.gridEnabled
window.scroll(0, 0)
Event.$emit('enableGrid')
}
}
})
Vue.component('grid', {
template: `
<div class="grid" v-if="gridEnabled">
<div class="grid-item aspect" v-for="(item, index) in info" @click="updateImg(index)">
<img v-lazy="item.img"/>
</div>
</div>
`,
created() {
Event.$on('enableGrid', () => this.gridEnabled = !this.gridEnabled)
},
data() {
return {
gridEnabled: false
}
},
computed: {
info() {
return info
}
},
methods: {
updateImg(index) {
this.gridEnabled = !this.gridEnabled
window.scroll(0, 0)
Event.$emit('updateImg', index)
}
}
})
new Vue({
el: '#app'
})
【问题讨论】:
-
如果您可以在此处包含代码 sn-ps 或提供一个小提琴示例(即jsfiddle.net)供其他人关注而不是发布您自己的网址会更好。
-
你能解释一下人字形和按键的行为有什么区别吗?
-
@StarkButtowski 我的不同之处在于,当我按下 V 形时,顶部的模糊图像不会改变,但它下面的英雄会 - 按键一切正常,图像相应地循环
标签: javascript html vue.js click