Creating interactive animations triggered by scrolling when using JQuery in combination with Bootstrap 3 modals

I am struggling to grasp the concept of Bootstrap modal div visibility, especially when they appear visible even when hidden.

The normal divs on the page, outside of the modal, function properly with my code.

...

I created some JS and CSS logic that dictates: if any element with the class .animation-element is within the window's view, add the class .in-view; otherwise, remove it. This should be straightforward, and it is, except for the divs inside the modal with the .animation-element class always showing as having the .in-view class.

...

var $animation_elements = $('.animation-element');
var $window = $(window);

function check_if_in_view() {
  var window_height = $window.height();
  var window_top_position = $window.scrollTop();
  var window_bottom_position = (window_top_position + window_height);

  $.each($animation_elements, function() {
    var $element = $(this);
    var element_height = $element.outerHeight();
    var element_top_position = $element.offset().top;
    var element_bottom_position = (element_top_position + element_height);

//check to see if this current container is within viewport
if ((element_bottom_position >= window_top_position) &&
    (element_top_position <= window_bottom_position)) {
  $element.addClass('in-view');
} else {
  $element.removeClass('in-view');
}
  });
}

$window.on('scroll resize', check_if_in_view);
$window.trigger('scroll');

and

.animation-element {
  opacity: 0;
}

.animation-element.in-view {
  opacity: 1;
  transition: 1s ease-in-out;
  transition-delay: 0.2s;
}

Even if the div with the data-target for the modal does not display .in-view because it's off-screen, any div inside the modal with the class .animation-element has the .in-view class. I have placed all modal divs at the bottom of the page, but they show the same outcome.

Check out an example on CodePen here: http://codepen.io/ethanethan/pen/MbbyoE

Answer №1

It's important to consider triggering the check_if_in_view() function when the modal is clicked or toggled. Fortunately, Bootstrap's JavaScript provides events specifically for modals (refer to the "events" section on this page).


...
$window.on('scroll resize', check_if_in_view);
$('#myModal').on('shown.bs.modal', function (e) {
  check_if_in_view();
});
$window.trigger('scroll');

You can modify #myModal to .modal to apply this functionality globally across your project. Keep in mind that only .animation-element within the modal should be checked during this event, so additional modifications may be necessary in the code.

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 Verify if JqxGrid Contains Any Errors?

Within the code below, I have successfully displayed highlighted borders when there are validation failures. Is it possible to determine any validation errors in a grid when a button is clicked? By clicking the button at the bottom, I would like to be able ...

Is there a way for me to retrieve the variable from one function and use it in another

I have a tool for managing images with descriptions that allows me to edit both the text and the image file. The challenge is saving the modifications I make in my database. Currently, I can only save changes if I modify the image itself. If I only update ...

I have the ability to see HTTP-only cookies within my web browser

So, I always believed that httpOnly cookies could only be accessed during a http request. But the other day, while inspecting Firefox dev tools, I noticed that I could actually view the cookies' values. Is this standard behavior? ...

How to set a specific text color when hovering over a link with jQuery

Seeking assistance in customizing a hover effect for spans within links. The current code applies the effect to all links with spans, rather than just the hovered item. Any tips on refining the code to target specific elements would be highly valued. $( ...

Retrieve webpage using HTML stored in web storage

I am exploring a new idea for an application that utilizes local storage, and I am seeking guidance on the most effective way to load content after it has been stored. The basic concept of the app involves pre-fetching content, storing it locally, and the ...

The process of extracting information from a JSON object and converting it into a dictionary in Python

By using the JavaScript code below, I am sending data from JavaScript to views.py: Javascript: $(document).ready(function(){ console.log("js2 working"); var jsonData={},a=0,key; $("#submit-button").click(function(e){ e.preventDefault(); $(&apos ...

Sending data from Ajax to PHP on the same page

<!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8"> <title>AJAX Testing</title> <!-- jQuery Library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> ...

Equal-Height Slide Deck Holder

I'm struggling to maintain a consistent height of 700px for my image slideshow, which consists of four images with varying heights. I attempted setting the img at max-height: 700px, but unfortunately that didn't yield the desired outcome. You ca ...

Clicking on the Material-UI slider that uses multiple useState causes duplication issues

Seeking help to develop a font size and font weight previewer utilizing Material-Ui sliders and useState. Encountering an issue where clicking the first slider after the second results in duplicating the first slider. https://i.sstatic.net/5MAzgtHO.png B ...

Avoiding Vue Select from automatically highlighting the initial option: tips and tricks

Currently utilizing Vue Select as a typeahead feature that communicates with the server via AJAX. By default, the first option from the server response is highlighted like this: https://i.stack.imgur.com/jL6s0.png However, I prefer it to function simila ...

Can a variable containing HTML code be iterated through with a loop?

I am currently working on setting up a select button that will receive options from an ajax call. The ajax call is functioning properly. However, I am facing a challenge when trying to create a variable containing the HTML. I am unsure of how to iterate a ...

Importing dynamically divides the code without implementing lazy loading

I am exploring the implementation of lazy loading in Vue Router to ensure that certain parts of the code are only loaded when they are needed. Following the guidelines from the official documentation on Lazy Loading in Vue Router available at: https://rou ...

Issues encountered with tsbs/bootstrap and the rendering of the templating engine

I'm currently facing a challenge in setting up a Bootstrap 4 development site on Debian 9. Despite installing dependencies and trying various methods, the default index page I receive has an empty head section. This absence of critical elements like j ...

Access external link without leaving current page

Dear webmasters, I am seeking advice, tools, or guidance to solve a simple problem. I have my own HTTP server that responds to HTTP requests. Example: If I visit the link: http://ip_server/link1, it performs a specific functionality If I visit the lin ...

Why do I keep receiving identical values for offset and position in jQuery?

Within the paragraph tag, I have inserted a span element. The expected behavior is for the position top to display as 0, while the offset should show a different value (which needs to be calculated from the document). However, I am encountering an issue wh ...

Is it possible to effortlessly update all fields in a mongoose document automatically?

Take for instance the scenario where I need to update a mongoose document in a put request. The code template typically looks like this: app.put('/update', async(req,res) => { try{ const product = await Product.findById(req.body.id) ...

Integrating Livefyre npm with Meteor

Currently, I am in the process of creating a custom package to integrate the livefyre npm module into Meteor after receiving a request from a client. Despite following the instructions provided here, I keep encountering errors that state Errors while scann ...

ESLint encountered an error while attempting to load the configuration "next/babel" for extension

Every time I try to generate a build of my Next.js app by running "npm run build," I keep encountering this error. Check out the error I'm getting while running npm run build here. Also, take a look at my .eslintrc.json file here and my .babelrc file ...

AngularJS, ExpressJS, and PassportJS are utilized for conducting authentication verifications on the server side

I have developed a Node.js application with Yeoman scaffolding for Angular and ExpressJS for server-side functionality. The structure of my app is as follows: app -->views --> more files server.js This is a simplified version of the actual ...

What could be causing the state to continuously appear as null in my redux application?

Currently, I am in the process of developing a basic contacts application to gain expertise in React and Redux. The main focus right now is on implementing the feature to add a new contact. However, I have encountered an issue where the state being passed ...