I have a specific task to create an effect where an image blinks three times on scrolling (like lights turning on and off consecutively with a 1-second delay), and then stays on until the user scrolls beyond 3600px.
To achieve this, I have added an event listener:
created() {
window.addEventListener('scroll', this.scrollAnimation)
}
This event listener triggers the method scrollAnimation
:
methods: {
scrollAnimation() {
let currentPos = window.pageYOffset
if (currentPos > 3000 && currentPos < 3600) {
this.$refs.supportOff.style.display = 'none'
this.$refs.supportOn.style.display = 'block'
} else {
this.$refs.supportOff.style.display = 'block'
this.$refs.supportOn.style.display = 'none'
}
}
}
Below is the HTML template for the images:
<div class="support__image-wrapper">
<img ref="supportOff" class="support__image support__image_on" src="../../assets/images/247-off.png">
<img ref="supportOn" class="support__image support__image_off" src="../../assets/images/247-on.png">
</div>
The code currently works when scrolling between 3000 and 3600 pixels, showing the appropriate images. However, there is an issue with creating the blinking effect using setInterval as it triggers every time the user scrolls within that range. What would be the best approach to implement the desired blinking effect?