The resizing effect in jQuery causes a blinking animation

I would like to create a functionality where, when the width of .tabla_gs_gm surpasses the width of .content, .tabla_gs_gm disappears and another div appears in its place. While this is already working, there seems to be a screen flicker between the two divs during resizing. I believe this issue is related to the resize function but I'm unsure how to address it.

jQuery:

<script>    
    function jqUpdateSize(){

        var content = $(".content").width();                                        
        var tabla_gs_gm = $(".tabla_gs_gm").width();

        if(tabla_gs_gm >= content){
            $(".grillas_mobile").css("display","block");
            $(".grillas_desktop").css("display","none");
        }else{
            $(".grillas_desktop").css("display","block");
            $(".grillas_mobile").css("display","none");
        } 

    };
    $(document).ready(jqUpdateSize);
    $(window).resize(jqUpdateSize);
</script>

HTML:

  <div class="content">
        <div class="grillas_desktop">
            <table>
                //content
            </table>
        </div>

        <div class="grillas_mobile">
            <table>
                //content
            </table>
        </div>
    </div>

CSS:

.grillas_desktop, .grillas_mobile{
    margin: 10px auto;
    width: 100% !important;
    height: auto;
    overflow: hidden;
    display: block;
}

table.tabla_gs_gm{
    margin: auto;
    width: 99%;
    border-collapse: collapse;
}

.tabla_gs_gm td{
    padding: 3px;
}

.tabla_gs_gm .class_label{
    display: block;
    vertical-align: middle;
    text-align: center;
}

.grillas_mobile select{
    margin: 3px 0px 10px 0px;
    width: 100%;
    padding: 3px 3px;
}

Answer №1

During a brief period, neither object is assigned the class resizing, resulting in some intriguing outcomes. You have the option to select any shared parent element for this situation, but I will opt for body. By adding a class to body that signifies whether resizing is occurring or not, you can ensure that the corresponding CSS rules are applied simultaneously:

<script>    
    function jqUpdateSize(){

        var content = $(".content").width();
        var tabla_gs_gm = $(".tabla_gs_gm").width();

        if(tabla_gs_gm > content){
            $('body').addClass('mobileResizing');
        }else{
            $('body').removeClass('mobileResizing');
        }   


    };
    $(document).ready(jqUpdateSize);
    $(window).resize(jqUpdateSize);
</script>

You can then style your CSS to respond to these events as follows:

When tabla_gs_gm > content is true, use body.mobileResizing as a prefix for your selectors:

body.mobileResizing .grillas_mobile {
  /* CSS for mobile when it's resizing */
}
body.mobileResizing .grillas_desktop {
  /* CSS for desktop when mobile is resizing */
}

And when tabla_gs_gm > content is false, utilize body:not(.mobileResizing):

body:not(.mobileResizing) .grillas_mobile {
  /* CSS for mobile when desktop is resizing */
}
body:not(.mobileResizing) .grillas_desktop {
  /* CSS for desktop when it's resizing */
}

You can include display: none; to hide or display: block; to show specific elements based on the aforementioned conditions.

Answer №2

I made a change to the height of ".tabla_gs_gm" so it no longer blinks when the content exceeds its size.

<script>    
    function adjustSize(){

        var content = $(".content").width();                                        
        var tabla_gs_gm = $(".tabla_gs_gm").width();

        if(tabla_gs_gm >= content){
            $(".grillas_mobile").css("display","block");
            $(".grillas_desktop").css("height","0px");
        }else{
            $(".grillas_desktop").css("height","auto");
            $(".grillas_mobile").css("display","none");
        } 

    };
    $(document).ready(adjustSize);
    $(window).resize(adjustSize);
</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

Using jQuery to dynamically select all rows (`tr`) that contain table data cells (`td`) matching

Although my title may seem confusing, it's the best I could come up with. I'm looking to identify all tr elements that contain td elements matching specific filter criteria. Here is an example: <tr class="row" id="1"> <td class="ph ...

Switching the placement of my menu bar to the other side of my logo

Can anyone help me figure out how to move my menu bar to the opposite side of my logo? I attempted using the position relative in CSS but it didn't reposition correctly. I've included the position:relative in the CSS code of my HTML index file, ...

Smoothly transition the box shadow using CSS3's ease-in and ease-out effect

Struggling to achieve a smooth easing effect for a box shadow transition using CSS3. This is the current CSS code in use: #how-to-content-wrap-first:hover { -moz-box-shadow: 0px 0px 5px #1e1e1e; -webkit-box-shadow: 0px 0px 5px #1e1e1e; box-s ...

Issues arise when attempting to run JQuery functions with the $ symbol in the head tag of HTML

Because of the limitations imposed by Squarespace, I am only able to include code via the Head tag. When the script reaches the $ part of JQuery, it seems to not execute at all. After testing with numerous console.log() statements, I observed that the webp ...

Modal window displaying MVC 4 partial view

I've been attempting to display a partial view using a popup, but it's not working. The controller method is also not being triggered. Here's my code: Script:- $(document).ready(function () { $.ajaxSetup({ cache: false }); ...

Is it possible to use velocity js to add a gradient color effect to text content?

Can I use velocity js to apply gradient color to text? I've looked at https://css-tricks.com/snippets/css/gradient-text/ My specific need is to dynamically add a changing gradient using js. Thank you in advance. ...

What is the best way to ensure that my text always aligns perfectly to the end of the line?

I've been struggling to create a column layout where the text perfectly aligns with the end of the line, but so far, I haven't had any success. Despite my efforts to find a solution online, I have come up empty-handed. What I'm aiming for i ...

Error encountered in Firefox: THREE.js throws a TypeError because 'global' is undefined

After attempting to integrate three.js into my code as a standalone script without using webpack, browserify or requirejs, I encountered an issue. My setup involves loading an external Angular app and extending it, but now I need my own version of THREE.js ...

Currently, my goal is to create a functional copy button through the use of JavaScript

I've been attempting to create a basic copy button using JavaScript, but I keep encountering an error. TypeError: null is not an object (evaluating 'myInp.select') Whenever I click the copy button, My code looks like this: <!DOCTYPE htm ...

The function json.stringify fails to invoke the toJson method on every object nested within the main object

When using IE 11, I encountered an issue where calling Stringify on my objects did not recursively call toJson on all objects in the tree. I have implemented a custom toJson function. Object.prototype.toJSON = function() { if (!(this.constructor.name == ...

Is it possible to locate the Bower configuration file in Debian if running as root?

Hey there, I've been trying to execute a "bower install" command on a project I'm working on, but unfortunately, I keep running into an error. Error details: Since bower is a user command, it shouldn't be run with superuser permissions. If ...

How can I retrieve a variable in a JavaScript AJAX POST request?

Similar Question: How to retrieve a variable set during an Ajax request I am facing a challenge, as I am making an ajax call and receiving a number as the response. My query is, how can I assign this returned number to a variable that is accessible ou ...

"Although all error messages are functional in Postman, they are not appearing correctly on the frontend client

I am currently developing a website using React and Node. While testing my register and login functionality in Postman, I encountered an issue where the error message for login (e.g., user doesn't exist, username or password incorrect) is not displayi ...

The Bootstrap CDN is malfunctioning and displaying errors in the console

I attempted to incorporate Bootstrap 4.5 using the CDN provided on their main website. However, after adding all the code lines to my project, I encountered numerous issues with my custom CSS and the Bootstrap carousel failing to function. Upon inspecting, ...

retrieve all the table data cells except for the initial one

I have a specific table structure that I need to work with: <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td&g ...

javascript Click to add a tag

I am currently using a rich text editor and I'm attempting to add a code tag feature to it. Currently, the editor has two buttons - one for underline and one for code. When I select text and click the underline button, the selected text will be under ...

The React.js component search test encounters errors in locating components

I have been working on a React app that utilizes Redux and React-router. I am currently writing tests using React TestUtils, and I encountered an issue with the following test results: The first expect statement is successful: expect(nav).to.have.length(1) ...

Utilizing Bootstrap to display selected portions of the list content

I am working with a select element that looks like this: jQuery('.my-select').selectpicker(); .list_img{ width: 30px; height: 30px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script ...

HTML Automated Email

I am currently working on developing a WIFI landing page that requires users to input their email address in a textbox. Upon clicking 'Connect', the entered email address will be sent to another designated email for review. While I have found co ...

Interactive window allowing the user to closely examine code

Hey guys, I need your help with something Is there a way (PHP / jQuery) that allows me to zoom in on my code? Similar to how lightbox works for images. I specifically want to zoom in on dynamic code while using Yii's CListView. I'm thinking of ...