Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding an element on pageLoad() with jQuery

If I hide some elements on page_load using jquery, the elements flicker for a split second when the page posts and then disappear:

  function pageLoad() {

        $('#panelOne').hide();
        $('#panelTwo').hide();

Is there a way to prevent the flickering?

I don't want to set the elements css to visibility: hidden, because then calling the jquery .show() method later on doesn't seem to show the element.

like image 900
Sean Thoman Avatar asked May 12 '11 22:05

Sean Thoman


People also ask

How do I hide the selected elements in a page?

The hide () method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show () method. Optional. Specifies the speed of the hide effect.

How to hide all heading elements when they are clicked in jQuery?

In this article, we will see how to hide all heading elements on a page when they are clicked in jQuery. To hide all heading elements from page, we use slideUp () method. First we use click () method to detect the element is clicked and then use slideUp () method to hide the heading elements.

How to hide content until the page loads?

I'm pretty sure $ ("#your-class-id").show (); would still work even hiding it that way as well. Add this to any element you don't want to be visible when loading the page. Then use $ ("div#extraControls").show (); to display it again. With CSS3 you can actually hide content until a page loads by taking advantage of the :only-child selector.

How to hide parts of text using jQuery?

How to hide parts of text. With jQuery, you can hide and show HTML elements with the hide () and show () methods: The optional speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", or milliseconds.


4 Answers

Setting visibility: hidden doesn't work, but display: none does. See jsFiddle.

You could do this on the DOMReady event, but it would be cleaner to do it in CSS.

like image 87
lonesomeday Avatar answered Sep 29 '22 17:09

lonesomeday


$.show() doesn't work on elements set to visibility: hidden. You need to use display: none. This will work out better for you than using jQuery to hide on DOM ready, and will absolutely guarantee you won't see flickering.

like image 36
Kevin Ennis Avatar answered Sep 29 '22 17:09

Kevin Ennis


Definitely use document ready as opposed to page load:

$(function() {
    $('#panelOne, #panelTwo')
        .hide();
});
like image 32
Eli Avatar answered Sep 29 '22 16:09

Eli


Rather than hide on pageload, hide it on domready like so:

$(function() { ........ });

Replace the ...... with your 2 lines of hiding.

Domready runs when the dom-tree has been built, and much earlier than pageLoad. Pageload waits for images and stuff to run. (asuming that you have pageLoad as following: <html onload="pageLoad();">).

like image 34
Alxandr Avatar answered Sep 29 '22 16:09

Alxandr