Enhance your Vue app by incorporating animate effects or classes when entering a div

Currently, I am working on a Vue project and I am looking to implement an effect or add a class to a specific div when the user scrolls to it. This functionality is similar to what can be achieved with ScrollReveal.

Has anyone encountered this scenario before while working with Vue?

Below is the CSS code that I have, which needs to be triggered when the specified div comes into view:

The title effect on h2 should take place when it becomes visible on the screen.

@keyframes slideInFromLeft {
    0% {
        transform: translateX(-100%);
    }
    100% {
        transform: translateX(0);
    }
}

h1 {
    animation: 1s ease-out 0s 1 slideInFromLeft;
}

h2 {
    animation: 1s ease-out 0s 1 slideInFromLeft;
}
<h1>
Hello World!
</h1>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<h2>
World Hello!
</h2>

Answer №1

To create the scroll to reveal effect, you can implement the use of the CSS class slide with animation properties set. This effect will trigger when the user scrolls to a specific position on the page. Here is an example code snippet:

new Vue({
  data: {
    el: document.getElementById('target-element')
  },
  methods: {
    isScrolledIntoView(el) {
      var rect = el.getBoundingClientRect();
      var elemTop = rect.top;
      var elemBottom = rect.bottom;
      isVisible = elemTop < window.innerHeight && elemBottom >= 0;
      return isVisible;
    },
    handleScroll() {
      /* check if element is scrolled into view */
      if(this.isScrolledIntoView(this.el))
        this.el.classList.add('slide');
    }
  },
  created() {
    window.addEventListener('scroll', this.handleScroll);
  },
});
@keyframes slideInFromLeft {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0);
  }
}

.slide {
  animation: slideInFromLeft 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<h1 class="slide">
Hello World!
</h1>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<h2 id="target-element">
World Hello!
</h2>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to prevent my menu from switching to mobile view if it can fully fit within the menu area on the device?

I've implemented a code on my website that changes the menu to "mobile" when the device screen resolution is less than 1024px. However, I noticed that the menu still displays correctly even when the device is around 800px. Is there a way to adjust thi ...

Extend the center of the image horizontally

Is there a way to horizontally stretch only the middle section of an image? Let's say I have this specific image: https://i.sstatic.net/pty5A.jpg (source: lawrenceinspections.com) I need the rounded corners to remain unchanged, so simply stretchi ...

First attempt at creating a website and experimenting with adding a secondary color background using CSS

As I embark on the journey of launching my website, I am faced with the challenge of working with CSS for the first time. I decided to purchase a custom theme from themeforest to kickstart my project. My goal is to have two background colors on my website: ...

The process of updating a session using jQuery

Whenever a checkbox is selected, the session gets populated with an id. However, if another checkbox is selected afterwards, the session remains unchanged. This is my current code structure: <input type='checkbox' class='imei' id=&a ...

Can the @keyframes in CSS3 be modified?

Exploring CSS3 animations has been a fun project for me, and I want to push the boundaries of what is possible. Currently, I have an infinite animation cycle that is working perfectly. However, I'm curious if it's possible to dynamically change ...

Struggling to display Firestore data in a React component - useRef() does not trigger re-render and useState() throws an error

I am currently working on a project involving a React component called Dashboard. The component includes various features such as loading data from a Firestore database and displaying it on the page. While implementing this functionality, I encountered an ...

Is it possible to embed HTML code within JavaScript strings?

Is my approach to inserting HTML into a JavaScript string correct? In particular, I want to insert an HTML link into a div tag using JavaScript. Here is the HTML: <div id="mydivtag"></div> And here is the JavaScript code: document.g ...

Iterating through elements to set the width of a container

I am currently working on a function that dynamically adjusts the width of an element using JavaScript. Here is my JavaScript code: <script> $('.progress-fill span').each(function(){ var percent = $(this).html(); if(per ...

What is the best way to ensure that messages stay saved in my app for an extended

I am looking for a way to store messages sent through my small messaging app in a persistent manner. Currently, the messages are transferred from the front-end to a Node, Socket.io, and Express back-end. A friend recommended using Enmaps (), but unfortuna ...

Directional Div CSS Transition

Is it feasible to determine the starting direction of a CSS transition? I've designed a div that enlarges in height when hovered over; however, it's situated at the bottom of the page, causing the scrollbar to extend downward. Ideally, I would ...

Latest Version of Joomla includes Jquery 3.4.3 Files

In my Joomla 3.4.3 setup, while debugging in Firefox using Inspect Element, I noticed a message in the console pointing to http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1.8.21/jquery-ui.min.js. Next to this item, it displays: [HTTP/1.1 404 Not Found ...

Error: jwt_decode function has not been declared

I'm currently working on a project and I've hit a roadblock while trying to fetch profile information for the logged-in account using a token. Despite using the JWT-decoding library, I keep encountering the issue where jwt_decode is not defined. ...

Using javascript, how can you fill in the missing dates within an array of objects?

I need to populate this object with dates starting from today up to the next 7 days. Here is my current object: let obj = { "sessions": [{ "id": 0, "available_capacity": 3, "date": "15-05- ...

Utilizing the selected option value from a drop-down menu in an input field

In my setup utilizing phpmyadmin within Xampp, I have configured two tables: Table 1: Category with columns cat_id and cat_name (where cat_id acts as the Primary Key) Table 2: Item with columns item_id, item_name, item_price ... cat_id (where item_id is ...

Issue retrieving WebElement inside iFrame using Selenium

I've been working on creating a small automated test script using the latest version of Selenium Chrome WebDriver. However, I have encountered an issue while trying to locate a WebElement within an iFrame, even after switching to it. Here is how the ...

I am dynamically populating two select dropdowns with data from two separate arrays

After setting up the first select list, the task at hand is to generate elements for each item in the "selectList.chains" array. Each element must consist of a label text node and a 'value' attribute before being appended to another element. This ...

Ways to implement multi-file loading on a website using separate HTML segments

Currently, I am working on a website for my local community. Each page contains similar content like header, navigation bar, and footer. I want to streamline this process by storing these common elements as separate HTML files. Is it possible to link the ...

Creating a Full-Width Form Using Bootstrap

I need help aligning 3 form boxes side by side with a total width of 100%. I want to be able to specify the width of each box, or have the last two boxes set to their default size while the first box fills up the remaining space. Here is my current code: ...

What are the best ways to integrate markdown into my Vue.js projects?

Is there a way to utilize markdown within the context of vue.js instead of regular HTML paragraphs? ...

Smartphone screen cluttered with overlapping paragraphs

I am encountering a paragraph overlap issue while using a bootstrap template, specifically on smartphones like iPhone 6 with Safari. Interestingly, the problem does not appear when testing on a browser by shrinking the window size to the minimum. On the ph ...