Any idea why divs are not fading in properly?

Seeking assistance to make these div's fade in, but they are not cooperating! Unsure if this is the most effective method, but aiming to acquire the skills needed to create animations similar to the ones on this website: When scrolling down the page, elements smoothly appear. It's truly impressive! If anyone knows of a resource or tutorial that teaches this technique, it would be greatly appreciated!


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
</head>    
<body>
<style type="text/css">
#container
{
    height:2000px;    
}    
#container DIV
{ 
    margin:50px; 
    padding:50px; 
    background-color:lightgreen; 
}    
.hideme
{
    opacity:0;
}
</style>
<script type="text/javascript">
$(document).ready(function() {        
    /* Every time the window is scrolled ... */
    $(window).scroll( function(){        
        /* Check the location of each desired element */
        $('.hideme').each( function(i){                
            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();                
           
            /* If the object is completely visible in the window, fade it in */
            if( bottom_of_window > bottom_of_object ){                    
                $(this).animate({'opacity':'1'},500);                        
            }                
        });         
    });        
});
</script>
<div id="container">        
    <div>Hello</div>
    <div>Hello</div>
    <div>Hello</div>
    <div>Hello</div>
    <div>Hello</div>
    <div>Hello</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>
    <div class="hideme">Fade In</div>        
</div>    
</body>
</html>

Answer №1

Is the jQuery properly loading? I had to add "https:" for it to load on my end:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
</head>

Your code worked perfectly once jQuery was loaded.

Answer №2

Is it more efficient to utilize fadeIn() rather than adjusting the opacity?

If you prefer not to use fadeIn(), consider adding a transition to the element.

div {
// your style
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
}

Then, apply the .css() method in your JavaScript code

$(element).css('opacity',1);

Additionally, opt for #container div instead of #container DIV (with lowercase letters) just to be safe.

edit: If you decide to go with fadeIn(), first, set an inline style for the element as display: none; and then execute

$(element).fadeIn('slow/fast/milliseconds')
This method actually yields better results.

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

Tips for transferring an array between pug mixins?

My pug template features an array that I want to transfer to a mixin in order to populate JSON schema.org markup. Below is the code: profile.pug include ../components/bio-seo - var person = {} - person.title = "Name, title of person" - person.url = "ht ...

Creating a Bootstrap 4 column form label with a maximum width limit

I am currently working on creating a form within a full-width fluid layout that consists of 2 columns - the label column and the inputs column. The issue I'm running into is that due to the wide screens on fluid layouts, the col-2 label appears too l ...

Enhance your web development experience with a dynamic jQuery editor for

Recently, I've been attempting to utilize a wysiwyg editor to modify content on newly generated div elements. Unfortunately, I have encountered an issue where the editor only recognizes pre-existing elements in the HTML and not the new ones. To create ...

Is there a way to extract the unicode/hex representation of a symbol from HTML using JavaScript or jQuery?

Imagine you have an element like this... <math xmlns="http://www.w3.org/1998/Math/MathML"> <mo class="symbol">α</mo> </math> Is there a method to retrieve the Unicode/hex value of alpha α, which is &#x03B1, using JavaScrip ...

Decoding JSON with HTML in c#

While serializing an object into JSON that contains HTML, I encountered a unique issue. Below is the structure of my class: [Serializable] public class ProductImportModel { public string ProductName { get; set; } public string ShortDescription { get; ...

Modifying data attribute values with jQuery

Hello there, I have a question regarding the following HTML element: <li class="total_payment" data-basetotal="0.00"> <span> <h5>Total</h5> </span> </li> I am trying to update the data-basetotal value f ...

Guide on placing a featured image in a div element within genesis theme

I'm encountering a minor issue when trying to work with the genesis framework. My objective is to have each post's featured image inside a div with the same class. I aim to be able to set a background image for the outer div so that when a user h ...

Animate with jQuery, starting from the bottom and moving upwards

Is it possible to animate the height of an element (such as a div) from the bottom to the top? For instance, consider a div with a height of 15px. <div class='greyBox'> <p>Example text</p> </div> Here is the correspondin ...

Generate a new array using a single value extracted from an existing array

I'm new to working with arrays. Before this, I used to manually code everything in HTML. My question is, can you create an array based on the values of another array? var pmmain =["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", ...

Encountering undefined JavaScript functions when called from HTML

I had most of these functions working perfectly, but after restarting my program, I'm now encountering issues with undefined functions in Chrome. I can't pinpoint the exact problem, and even though I've checked through Eclipse, I still can&a ...

"Double triggering of window scroll event observed in IE8, occurring two times instead of a

Every time I reach the bottom of the page in IE8 (except occasionally on the first attempt), 'log' is displayed twice in the console instead of once. This issue is specific to IE8, as newer versions of IE and other standard browsers function corr ...

Arrangement of box and buttons on R shiny interface

My dashboard page features action buttons aligned to the left, along with a plot and two additional zoom and reset buttons. I am looking to center the box on the screen and position the zoom and reset buttons at the top right corner. I attempted to use t ...

``In JavaScript, the ternary conditional operator is a useful

I am looking to implement the following logic using a JavaScript ternary operation. Do you think it's feasible? condition1 ? console.log("condition1 pass") : condition2 ? console.log("condition2 pass") : console.log("It is different"); ...

Is it possible to use Selenium WebDriver to automate an HTML5 webpage?

Within my application, there lies an HTML5 webpage. While attempting to inspect it and retrieve IDs/classname locators, I have encountered difficulties in obtaining the necessary locators for specific web elements. I have noticed that many of these elemen ...

Building a collapsible toggle feature with an SVG icon using HTML and CSS

I am trying to swap a FontAwesome Icon with a Google Materials SVG Icon when a collapsible table button toggle is pressed (changing from a down arrow to an up arrow). I have been struggling to get the Google Material Icons code to work. How can I resolve t ...

Prevent repetition of design during a partial reload of the page

In my ASPX pages, I incorporated a partial postback. Some elements are styled using CSS to indicate whether they are mandatory to fill in. However, upon performing a partial postback without inputting anything, the styling within my updatepanel disappears. ...

Are there any methods I can employ to maintain the traditional aesthetic while incorporating modern elements into web forms?

My current webforms application was built using visual basic, but now the user wants to migrate it to a new UI with Bootstrap and modern styling. The goal is to apply a side navigation bar and upper navigation bar while preserving the old pages and their o ...

Exploring the power of Angular by implementing nested ng-repeat functionalities:

I am currently working on an ng-repeat feature to add items from the array (album array). Everything seems to be functioning correctly. However, I also have a colors array that should assign different background-colors to the card elements of the album arr ...

Div not growing to accommodate images

I've attempted to use the overflow:hidden; property, but it didn't have the desired effect. Essentially, my div is structured like this, and when I add more images, it doesn't expand along with them. This is how the div appears: https:// ...

The media query appears to be ineffective on the footer section when accessed from a mobile device, yet it functions properly when adjusting the browser's screen size

I'm facing an issue with the footer section of my website. It appears very responsive when viewed from a desktop or tablet, but when I open the site on my mobile phone, the footer looks unstyled. Here is how it appears on desktop at 360px: enter ima ...