The "flickering" effect of the div is like a dance, moving gracefully between fade outs and

I am encountering a similar issue as described in this thread: same problem (fiddle: original fiddle). However, I am still learning JavaScript and struggling to apply the solution provided because my HTML code is slightly different. Can someone please assist me with this? Once I see how it should be done, I can use it as a learning opportunity.

This is my current code:

<div style="position: absolute; left: 50%;">
    <div style="position: relative; left: -50%;">
        <div id="101" style="display:none;"><!-- Content here --></div>
        <div id="102" style="display:none;"><!-- Content here --></div>
        <div id="103" style="display:none;"><!-- Content here --></div>
        <div id="104" style="display:none;"><!-- Content here --></div>
        <div id="105" style="display:none;"><!-- Content here --></div>
        <div id="106" style="display:none;"><!-- Content here --></div>
        <div id="107" style="display:none;"><!-- Content here --></div>
        <div id="108" style="display:none;"><!-- Content here --></div>
        <div id="109" style="display:none;"><!-- Content here --></div>
        <div id="110" style="display:none;"><!-- Content here --></div>
        <div id="111" style="display:none;"><!-- Content here --></div>
        <div id="112" style="display:none;"><!-- Content here --></div>
        <div id="113" style="display:none;"><!-- Content here --></div>
    </div>
</div>

<script type="text/javascript">

    $("#1").on('click', function() {
    $("#101").fadeToggle();
    $("#102,#103,#104,#105,#106,#107,#108,#109,#110,#111,#112,#113").fadeOut();
    });

    $("#2").on('click', function() {
    $("#102").fadeToggle();
    $("#101,#103,#104,#105,#106,#107,#108,#109,#110,#111,#112,#113").fadeOut();
    });

    // and so on, until #13 
</script>

Solution:

$('#indexNav').click(function() {
$('#aboutContent').fadeOut('fast',function(){
    $('#indexContent').fadeIn('fast');
});
return false;
});
$('#aboutNav').click(function() {
$('#indexContent').fadeOut('fast',function(){
    $('#aboutContent').fadeIn('fast');
});
    return false;
});

Answer №1

That's quite a bit of code to review. I'm not entirely sure if this will fix your issue. Since there are no hover events included, the flickering may be related to CSS.

// Consider using a better selector - the markup doesn't have #1, #2, #3, etc.
// Can you modify this to target $('.container div') or something based on the actual markup?
$('div').on('click', function() {

  // Get the ID of this element
  var id = $(this).attr('id');

  // Locate the corresponding div ID, which appears to be 100 greater than the original ID
  $('#1'+id).fadeToggle()
      // Hide all sibling elements
      .siblings().fadeOut();

});

I've provided a snippet for testing below.

$(function() { // Equivalent to onDomReady
  $('button').on('click', function() {
    // Get the ID of this element
    var id = $(this).attr('id');

    // Locate the corresponding div ID, which appears to be 100 greater than the original ID
    $('#1' + id).fadeToggle()
      // Hide all sibling elements
      .siblings().fadeOut();

  });
});
div div div {
  border: 1px solid black;
  margin: 3px;
  background: red;
}
<button id="01">01</button>
<button id="02">02</button>
<button id="03">03</button>
<button id="04">04</button>
<button id="05">05</button>
<button id="06">06</button>
<button id="07">07</button>
<button id="08">08</button>
<button id="09">09</button>
<button id="10">10</button>
<button id="11">11</button>
<button id="12">12</button>
<button id="13">13</button>

<div style="position: absolute; left: 50%;">
  <div style="position: relative; left: -50%;">
    <div id="101" style="display: none;">101</div>
    <div id="102" style="display: none;">102</div>
    <div id="103" style="display: none;">103</div>
    <div id="104" style="display: none;">104</div>
    <div id="105" style="display: none;">105</div>
    <div id="106" style="display: none;">106</div>
    <div id="107" style="display: none;">107</div>
    <div id="108" style="display: none;">108</div>
    <div id="109" style="display: none;">109</div>
    <div id="110" style="display: none;">110</div>
    <div id="111" style="display: none;">111</div>
    <div id="112" style="display: none;">112</div>
    <div id="113" style="display: none;">113</div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Update: The issue you're facing is when both divs (one fading out and one fading in) are toggling at the same time. To resolve this, consider positioning those divs absolutely. This way, they overlap each other and eliminate the flickering. However, keep in mind that this can significantly impact the layout. For more information, refer to this link.

Answer №2

Isn't that interesting? By utilizing promises and employing fadeToggle only when all elements were hidden, I successfully eliminated the flickering effect.

$(function() { // onDomReady equivalent
  $('button').on('click', function() {
    // obtain the id of this element
    var id = $(this).attr('id');

    // locate the corresponding div ID, which appears to be 100 greater than the original ID
    $.when($('#1' + id).siblings().fadeOut()).done(function() {
        $('#1' + id).fadeToggle();
    });
  });
}); 
div div div {
  border: 1px solid black;
  margin: 3px;
  background: red;
}
<button id="01">01</button>
<button id="02">02</button>
<button id="03">03</button>
<button id="04">04</button>
<button id="05">05</button>
<button id="06">06</button>
<button id="07">07</button>
<button id="08">08</button>
<button id="09">09</button>
<button id="10">10</button>
<button id="11">11</button>
<button id="12">12</button>
<button id="13">13</button>

<div style="position: absolute; left: 50%;">
  <div style="position: relative; left: -50%;">
    <div id="101" style="display: none;">101</div>
    <div id="102" style="display: none;">102</div>
    <div id="103" style="display: none;">103</div>
    <div id="104" style="display: none;">104</div>
    <div id="105" style="display: none;">105</div>
    <div id="106" style="display: none;">106</div>
    <div id="107" style="display: none;">107</div>
    <div id="108" style="display: none;">108</div>
    <div id="109" style="display: none;">109</div>
    <div id="110" style="display: none;">110</div>
    <div id="111" style="display: none;">111</div>
    <div id="112" style="display: none;">112</div>
    <div id="113" style="display: none;">113</div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Error message reading "Unexpected type error in jQuery for Node. Argument 'context' required."

Currently facing an issue with deploying my node.js server online. While everything functions properly on my local machine, I encountered an error when attempting to run the server online using node: node.js:201 throw e; // process.nextTick error, ...

There is a method in TypeScript called "Mapping IDs to Object Properties"

I'm currently developing a React app that features several input fields, each with its own unique id: interface IFormInput { name: string; surname: string; address: string; born: Date; etc.. } const [input, setInput] = useState< ...

Locate the parent and child elements within an array

Within the array provided below, there are parent items as well as children. I am currently able to identify parents (depth 0) and their immediate children (depth 1), however I am unsure how to handle deeper nested levels. For reference, you can view the ...

An array is not just a mere collection of elements

I have an object that resembles an array var items = [ { started_time: 2017-05-04T12:46:39.439Z, word: 'bottle', questionId: '161013bd-00cc-4ad1-8f98-1a8384e202c8' }, { started_time: 2017-05-04T12:47:26.130Z, word: &apo ...

Access model information from a controller with the help of Next.js and Sequelize

I'm currently working on a project involving Vue.js as my frontend and Next.js as my backend. While everything seems to be running smoothly, I am facing an issue with retrieving my model data back to my controller... Additional details: I have utili ...

Using a try block inside another try block to handle various errors is a common practice in JavaScript

In an effort to efficiently debug my code and identify the location of errors, I have implemented a try-catch within a try block. Here is a snippet of the code: for (const searchUrl of savedSearchUrls) { console.log("here"); // function will get ...

Creating a toggle feature using ng-switch

I'm having trouble getting this code to function correctly as a toggle. Can anyone offer any suggestions on what might be causing the issue? <div ng-switch="!!myvar"> <a ng-switch-when="false" ng-click="myvar = true" style="cursor:poin ...

Unequal height among each div's elements

I'm struggling with creating a footer divided into 3 blocks, but they do not have equal heights, causing the border-right line to appear uneven. Here is a screenshot of the issue: Not equal height of elements Any suggestions on how to fix this? Is m ...

Is there a way to synchronize breakpoint values across different media queries?

Currently utilizing Next.js version 9.53. Utilizing the built-in CSS module support provided by Next.js without the use of CSS pre-processors (less, sass, etc.) or CSS-in-JS (styled-components, etc.). Presently, my approach involves writing CSS in a mobi ...

Spirit.py navigates using javascript

Having trouble with Ghost.py. The website I'm trying to crawl uses javascript for paginated links instead of direct hrefs. When I click on the links, selectors are the same on each page so Ghost doesn't wait since the selector is already present. ...

Why isn't res.send() in Node.js sending the message?

Despite numerous attempts, I am unable to send my response message. It was working briefly, but after some code changes, it stopped functioning. Can anyone provide a solution to this challenging issue? exports.listBoth = function(req, res) { ...

"How to Develop an Eraser Tool Using EaselJs - Exploring Further Possibilities

Currently, I am working on creating a Bitmap eraser on easelJS. I feel like I've made significant progress towards achieving this goal, but I'm still a bit unsure of the next steps... Here is a demo of my progress so far: http://jsfiddle.net/bor ...

Is it possible to create Excel documents containing statistical graphs and pie charts by utilizing PHP and SQL?

I have a database filled with statistical data that I want to export into an excel file. Can anyone recommend any popular libraries or scripts for generating excel files? Additionally, I am interested in displaying some of the dry numerical data in p ...

Retrieve nested JSON data from an AJAX request

Currently, I am working with an API that provides JSON data back to me. The challenge I'm facing is figuring out how to access this data and showcase it on my HTML webpage since the results are stored in server memory rather than a file. Below is an e ...

Responsive Navigation Bar with Bootstrap

Struggling with the Bootstrap 4 navbar issue, specifically trying to close it when the <a> within the <li> tag is clicked. Experimented with data-toggle="collapse" data-target=".navbar-collapse.show" inside the <a> tag and it worked, but ...

Exploring the Efficiency Enhancements in the next/image Optimization Process within Next.js

I've been delving into Next.js and using the next/image component for image rendering. While leveraging it to enhance image loading in my project, I've become intrigued by the intricate workings behind the scenes. I'm particularly interested ...

Encountering a Tailwindcss error while attempting to compile the CSS file

Struggling to grasp Tailwind CSS as I try to compile the CSS but keep encountering errors. Despite following numerous tutorials, this persistent error continues to hinder my progress. This is the specific error message that keeps popping up: $ npm run bui ...

I'm having trouble getting the float left to work properly in my HTML/CSS code

Currently, I am diving into the world of HTML / CSS on my own. After following some tutorials, I have taken the leap to work on my very first project. My goal is to layout three images and three text elements on a single page using CSS. The desired struct ...

Ways to retrieve the checkbox value using PHP in conjunction with XML

I am currently in the process of learning PHP, XML, AJAX, and other related technologies. I have a form that is working fine for the most part, but I am facing an issue with passing the value/state of a checkbox to my PHP script. It's worth mentionin ...

Upon installing a global npm package, the system encountered an error stating: 'File or directory not found: ENOENT'

After successfully publishing my first Node.js CLI tool package on npm, I encountered an issue when trying to test it by installing it locally. The warning message "Error: ENOENT: no such file or directory" kept showing up. Steps for Reproduction To start ...