How to convert JQuery to Vue.js Script
Add the scroll event handler in the mounted()
function and change the isSticky
variable there.
export default {
data: () => ({
isSticky: false,
}),
mounted() {
this.scroll_event_handler = () => {
this.isSticky = window.scrollY > 50;
}
window.addEventListener("scroll", this.scroll_event_handler);
},
unmounted() {
window.removeEventListener("scroll", this.scroll_event_handler);
},
}
Then in your template you can add/remove the class like this:
<nav class="navbar-fixed-top" :class="{'top-nav-collapse': isSticky}">
...
</nav>
how to convert Jquery line of codes into vue
You don't need to convert anything. You just need to read the first few chapters in the Vue documentation.
var app = new Vue({
el: '#app',
data() {
return {
active: 1,
step: ['step 1', 'step 2', 'step 3'],
}
},
})
.activeClass{
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<ul>
<li
v-for="(items, index) in step"
:key="items"
:class="(index === active) ? 'activeClass' : ''"
v-text="items"
/>
</ul>
</div>
Related Topics
Chat Box, Auto Scroll to Bottom
How to Parse Xml File in React
Validate Phone Number With Yup
React Native: Image Not Displaying With Dynamic/Local Uri
How to Mock a Fake Database for When Unit Testing Against Knex
Angular: Toggle Active Class on Only Button the Current Clicked Button (Not Using *Ngfor)
Javascript Generate Random Numbers Without Repeating
Keep Order of Objects Inside a Json String After They Are Parsed
Uncheck a Checkbox and Another Checkbox Will Untick
Preventing Viewport Resize of Web Page When Android Soft Keyboard Is Active
Angular 4: Cannot Instantiate Cyclic Dependency! Injectiontoken_Http_Interceptors
Jquery Ui Tabs - How to Get Currently Selected Tab Index
Check If a Number Has Repeated Digits
React Passing Import Name as Prop and Adding to Image Src
Jquery Wait Until Ajax Call Has Got Data Before Displaying
Different Color for Each Bar in a Bar Chart; Chartjs
Get Initials and Full Last Name from a String Containing Names