What steps can I take to prevent Javascript/JQuery from repositioning other elements on the webpage?

Is there a way for the animation code to only affect the show/hide toggle buttons and not interfere with the other jQuery code I have implemented for hiding and showing div text? When I included the animate code in my js page, it caused issues with the existing show/hide functionality.

$(document).ready(function() {
  $("#hide").click(function() {
    $("#data").hide();
    $('#show').show();
    $('#hide').css('display', 'none');
  });
  $("#show").click(function() {
    $("#data").show();
    $("#hide").show();
    $('#show').css('display', 'none');
  });
});

$(document).ready(function() {
  $("#button").click(function() {
    var div = $("div");
    div.animate({
      height: '300px',
      opacity: '0.4'
    }, "slow");
    div.animate({
      width: '300px',
      opacity: '0.8'
    }, "slow");
    div.animate({
      height: '100px',
      opacity: '0.4'
    }, "slow");
    div.animate({
      width: '100px',
      opacity: '0.8'
    }, "slow");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button id="hide">Hide</button>
<button id="show">Show</button>
<div id="data">If you click on the "Hide" button, I will disappear.</div>
<button id="button" style="position:absolute;top:500px;left:0px;">Start Animation</button>
<div id="div" style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>

Answer №1

Make sure to use the id="div" selector as $("#div")

Check that your buttons are not overlapping by removing position:absolute from the <button> style

If you want to hide the text div, you can comment it out or remove //$("#data").show(); from the click event of $("#show")

$(document).ready(function() {
  $("#hide").click(function() {
    $("#data").hide();
    $('#show').show();
    $('#hide').css('display', 'none');
  });
  $("#show").click(function() {
    //$("#data").show();
    $("#hide").show();
    $('#show').css('display', 'none');
  });
  
  $("#button").click(function() {
    var div = $("#div");
    div.animate({
      height: '300px',
      opacity: '0.4'
    }, "slow");
    div.animate({
      width: '300px',
      opacity: '0.8'
    }, "slow");
    div.animate({
      height: '100px',
      opacity: '0.4'
    }, "slow");
    div.animate({
      width: '100px',
      opacity: '0.8'
    }, "slow");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button id="hide">Hide</button>
<button id="show">Show</button>
<div id="data">If you click on the "Hide" button, I will disappear.</div>
<button id="button" style="top:500px;left:0px;">Start Animation</button>
<div id="div" style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>

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

Modifying Element Values with JavaScript in Selenium using C#

As a newcomer to automation, I am facing a challenge with automating a web page that has a text field. I initially attempted using driver.FindElement(By.XPath("Xpath of elemnt").SendKeys("Value"); but unfortunately, this method did not work. I then resor ...

Instructions for encoding a video using PHP, saving the encoded video to a file, and then decoding and storing it in a database

I have successfully encoded a video in PHP using HTML, and now I am trying to figure out how to decode and save it in PHP. However, I'm uncertain about the process of saving the encoded video and how to access it for decoding. Any assistance would be ...

Tips on saving every query outcome in a separate array and delivering it back to the controller upon completion

I am currently facing an issue where I receive data in a function from my controller, and inside my model function, I need to retrieve results using a query with a dynamic value of channel. The channel ID will be coming from each checkbox on my HTML view ...

What could be causing this JSON object error I'm experiencing?

res.send({ customerDetails:{ fName, lName, }, applicantDetails:{ [ {primaryApplicant:{fName1,lName1}}, {secondaryApplicant:{fName2,lName2}}, {thirdA ...

HTML/Bootstrap - carousel malfunctions and no longer functional

Once I had a beautiful carousel scrolling through 4 images flawlessly. I made some updates to another section of the website, and now the carousel has stopped responding altogether. It doesn't cycle through the images automatically, and the buttons or ...

Remove the active class from a list item using History.js

My goal is to add the active class to a link when clicked in my AJAX app, triggering statechange on History.js. I'm struggling with saving the current active link(s) with the state so that the active class is appropriately added or removed when naviga ...

Creating an image on an HTML canvas using RGB values

Looking for assistance on displaying an image using HTML Canvas with three 12x12 arrays containing R, G, and B values. I've seen Canvas demos showing how to draw lines, but nothing on using RGB arrays to create an image. Any tips or guidance would be ...

What are some effective ways to integrate the WordPress API with ReactJS?

Wordpress recently introduced an API that allows you to make HTTP requests without worrying about routes, as the backend is handled for you. I'm curious, how can I integrate ReactJs with Wordpress API? This has been a frustrating challenge for me be ...

JavaScript and the infamous Internet Explorer back button

After performing a test, it was discovered that when pressing the back button in Safari, the javascript state of the previous page is saved. However, in IE9, this does not seem to happen. Is this just one of those quirks that comes with using IE, or is t ...

What is the most effective way to show the current date on a webpage: utilizing JavaScript or PHP?

Looking to show the current date and time on a webpage. There are two potential methods to achieve this: (1) Using PHP, for example: <?php echo date('Y-m-d H:i:s');?> (2) ... or JavaScript, like so: <div> <script>document.wri ...

guide on displaying all json elements using javascript

I am struggling to create a card element for each row in the database using my function, but it only prints the first element. Can you help me identify what I forgot to include? (the query is working correctly) aggiornaEventi(); function aggiornaEventi ...

Dynamic Data Binding in Ionic 2

One challenge I am facing is with creating my own info window for a Google Maps marker. Whenever I click on the marker, a div is displayed but the information inside the div does not get updated. It seems like the event.title remains unchanged from its old ...

CSS - Implementing responsive design to ensure optimal viewing experience on all devices

Card Matching Game Project Codepen Check out my CSS here, and find the rest of the code on Codepen since it's quite lengthy. const cards = document.querySelectorAll('.memory-card'); let hasFlippedCard = false; let lockBoard = false; let ...

Enhancing x-axis presentation in D3.js-generated charts

I created a bar chart using D3.js, but I have encountered an issue with one of the values on the x-axis being too long. I attempted to use CSS properties like text-overflow: ellipsis, width: 10px, and overflow: hidden to abbreviate values that exceed a cer ...

Obtaining a variable from within two nested functions

I'm looking for a solution where I can retrieve a variable from inside a function using an ajax call. For example: function returnStuff(){ $.ajax({ //do something }).done(function(response){ return response; }) return response; } The ...

Eliminate the border and style of a link that was clicked before in a React/Next.js

I've created a function where clicking on an element adds borders using onClick trigger. Here's the code snippet from the component: import { useEffect, useState } from 'react'; import Link from 'next/link'; import Logo from ...

Issue with Image Transition while Linking

I have been attempting to create transitions between images using links. I followed the steps in 'Demo 6' from this page (http://css3.bradshawenterprises.com/cfimg/), but unfortunately, it did not work as expected on my testing website (danitheme ...

Pagination in the style of a book using HTML

Currently, I am faced with a unique challenge. I have a large HTML file that contains images and links, and I am looking to parse it in order to display it like a book with interactive paging (using swipes). Has anyone encountered a similar task in iOS bef ...

JavaScript Transforming an Array into an Object

After working with node and redis for a while, I've encountered an issue. When using hgetall in redis, it returns an object. { uid: '6203453597', first_name: 'Name', last_name: 'Surname', gender: 'male& ...

Every time I try to retrieve my JavaScript code, I am bombarded with endless prompts that prevent me

Desperate for assistance! I have been using a website called "codepen" for my javascript coding, but I mistakenly triggered infinite prompts every time I open the project. Now, I am unable to access the code and despite my efforts in searching for a solu ...