Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page and undo those changes when they scroll back up. The issue I'm encountering is that while the divs shrink, slide, etc., once the scroll bar hits a specific point, the animations do not revert when scrolling back up. The problem seems to be centered around ".animate," as other events work perfectly fine. Below is a snapshot of my HTML code:

<!DOCTYPE>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Sarah's Portfolio</title>
        <link rel="stylesheet" type="text/css" href="main.css">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="navigationbar.js"></script>
    </head>
    <body>
        <div id="image">
            <img src="Project Images\\Sarah.png" id="Sarah">            
        </div>
        <div id="name">
            <h1>Sarah James</h1>
            <h2 id="objective">OBJECTIVE</h2>
        </div>
        <table id="navbar" cellspacing=0>
            <tr>
                <td><u><i><a href="index.html">Home</a></u><i></td>
                <td><a href="resume.html">Resume</a></td>
                <td><a href="portfolio.html">Portfolio</a></td>
                <td><a href="contact.html">Contact Me</a></td>
            </tr>
        </table>
        <div id="topbar"></div>
        <div id="rotatingblock" align="middle">
            <img width=90% height=50% class="mySlides" src="Project Images\\AsburyPoolComplex\\page-4-1.jpg">
            <img width=90% height=50% class="mySlides" src="Project Images\\AsburyPoolComplex\\page-5-1.jpg">
            <img width=90% height=50% class="mySlides" src="Project Images\\AsburyPoolComplex\\page-6-1.jpg">
        <script src="rotationblock.js"></script>
        </div>
    </body>
</html>

And here is my jQuery snippet:

$(document).ready(function(){
    $(window).scroll(function () {
        var $scroll=$(window).scrollTop();
        if ($scroll>60){
            console.log ("bigger than 40 and is " + $scroll);
            $("#name").animate({top:"2%",height:"13%",left:"35%"},500);
            $("#image").animate({width:"8%",left:"5%",top:"3%"},500);
            $("#navbar").animate({top:"-17%"},500);
            $("#topbar").animate({height:"25%"},500);
            $("#objective").html("<h2></h2>");
            $("td").animate({height:"20%"},500);
            $("a").css("font-size","150%");
        }
        else{
            console.log ("smaller than 40 and is " + $scroll);
            $("#name").animate({height:"21.5%",left:"40%"},500);
            $("#image").animate({width:"15%",left:"0%",top:"2%"},500);
            $("#navbar").animate({top:"1.3%"},500);
            $("#topbar").animate({height:"43%"},500);
            $("#objective").html("OBJECTIVE");
            $("td").animate({height:"20%"},500);
            $("a").css("font-size","175%");
        }
    })
})

Answer №1

One way to enhance your current method is by specifying explicit declarations for the options targeted in your animate functions. This allows you to easily add more options and structure your code like this:

{ duration: whateverMilisecs, queue: false }

For example:

$("td").animate({height:"20%"},{ duration: 500, queue: false });

Another approach could be using CSS transitions for a simpler animation process.

Add a .anim class to the objects you want to animate with these options:

.anim{
  -webkit-transition: all 1s; // Chrome
  -moz-transition: all 1s; // Mozilla
  -o-transition: all 1s; // Opera
  transition: all 1s;
}

Identify the option you wish to animate with another class:

.doThingy{
  transform: translate( 0px, -500px); 
}

Then, in your conditional statements:

if(etcetc)
  $(target).addClass('doThingy');
} else {        
  $(target).removeClass('doThingy);
}

This method utilizes CSS for defining animations on elements and toggling classes to trigger these animations when needed.

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

Adjust the size of the image obtained from the JSON data

I am currently working on parsing JSON data which includes an image field that I want to adjust to a specific size. Despite my many attempts at tweaking the code, I seem to have lost track of why it is not functioning as desired. JSON var items=[]; ...

Implementing dynamic loading with a Vue component loader

Is it possible to dynamically import a component using a default Loader if it's not loaded? Currently, we are using the following approach: import Dashboard from '../components/dashboard'; Vue.component('dashboard', Dashboard); ...

Add a directive on the fly, establish a connection, and display it dynamically

Within my markup, I have a tag element called popup-window which is handled by a specific directive. If I wish to incorporate multiple similar widgets that can be displayed or hidden in various locations, I currently need to include all these elements dire ...

What is the best way to retrieve content from an iframe with jquery?

I have added an iframe tag to my HTML page and am trying to conceal certain contents within that iframe. The content in the iframe is from a different origin. I attempted to use $("#iFrame").contents() but it is not functioning as expected. Could this be ...

Fluid percentage-based layout

I have four separate Divs that I would like to position on my page in a specific layout. Here is an image showing how I want them positioned: In the future, I plan to add multiple svg files to Div 4 and I want it to be scrollable. Please note that I only ...

Adjusting Iframe Dimensions Dynamically with Javascript based on Anchor Location

I am experienced with handling this issue in flash, but I am struggling to figure out how to do it using Javascript and CSS. The problem involves an iframe that does not have an overflow property, and I need to modify its width/height. Is there a simple ...

Enable users to modify the URL in the window.open() function

In the code snippet below, a new window opens when a user clicks on a button: $('.loginButton').click(function () { var strString = $("#medlineSearch").val() var strSearch = "http://google.com&query="; var url = strSearch + strSt ...

Error encountered: Multer TypeError - fields does not have a function called forEach

I'm currently working on a metadata reader using multer in node.js with express on c9. I've spent some time searching for solutions to my issue, but it seems like no one else has encountered the error I am facing: TypeError: fields.forEach is no ...

Ways to adjust the position of a DIV based on its index value

I'm currently working on a unique project that involves creating a triangular grid using HTML and CSS. The challenge I am facing is offsetting each triangle in the grid to the left by increasing amounts so they fit seamlessly next to one another. Righ ...

Discovering the offset location using touchmove

Struggling with a code for dragging/moving an element via touchscreen. The code is functional, but encounters a common issue - unable to drag from the touchpoint itself. Every movement initiates from the edge of the element, no matter where the touch begin ...

Executing a function on each page within the head tag in Nuxt.js

I successfully made my function accessible by using the plugin attribute in the nuxt.config.js file, allowing me to call the function under mounted on every page. I am looking for a more efficient way to run this function within the head tag and have it b ...

Data is nowhere to be found despite using the post method, but the URL still contains valuable information

Recently, I've delved into the world of PHP and have been experimenting with HTML forms. Initially, I used the method GET which worked perfectly fine. However, when I switched to using the method POST, I encountered an issue where I couldn't retr ...

Halt the execution of a function upon clicking a div element

I'm currently working on a function that needs to be stopped when a div with the class "ego" is clicked. This function toggles the visibility of the header based on the scroll position and should only run by default. Below is the code snippet: $("#e ...

unable to show image retrieved from JSON data

Looking to showcase the data from my JSON objects, which are displayed in 2 images below https://i.stack.imgur.com/yhqFy.png https://i.stack.imgur.com/DUgFO.png In the data, I have properties like c_name, max_slots, and an array slots. Within the array, ...

Updating the value on button click using Rails 5, Ajax, and jQuery

Whenever the user clicks on the "Add to Cart" button, I want to update the number in my navbar div to display how many products are currently in their cart. To achieve this, I have set the remote: true attribute on the button. The navbar contains a div w ...

"Text that appears on a clear background when you hover over it

I'm attempting to have hidden text display inside a table cell upon hovering. I managed to achieve the hidden-until-hovered effect, but I am struggling to make the text appear solid in color (it currently decreases in opacity when hovered over). Is th ...

In an array where the first 3 images have been filtered using an if statement, how can I show the image at the 3rd index (4th image) starting from the beginning?

In my personal web development project, I'm using AngularJS for the front-end and .NET Core for the back-end to create a website for a musical instrument retailer. The model I'm working with is called "Guitar" and all the guitar data is stored in ...

Display HTML content using AJAX within a div element

Within my HTML code, I have the following: <li class="register"><a href="">Registreer</a></li> as well as <div id="content"> </div> I attempted to load an HTML file into the div using the code below in the header se ...

Using JavaScript's setInterval function in conjunction with Math.random to obtain a random value

Just generated some random numbers. Wondering how to dynamically update the values of these numbers every 5 seconds with fluctuations less than +/- 5% compared to the current value. Need help using the setInterval function. Here is my code for Random Pric ...

Utilizing React with a Bootstrap Select element populated by an API call. Following form submission, I aim to automatically deselect the previously selected

After submitting the form, I am trying to reset the state to an empty value for the dropdown menu, but the selected item still appears before submitting the form. Any assistance in identifying the issue would be greatly appreciated. Thank you. Please see ...