Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with showing/hiding divs

I am trying to show some random images in a div. My HTML code is like this:

<div class="sponsors">
<div id="sponsorsContent">
    <div class="spacer"></div>
    <div class="sponsorSlide" id="imgageSlide_1">
        <img alt="" src="<?php echo $this->baseUrl(); ?>/img/Sponsors/img_1.jpg" /><br />
        <span class="thumbnail-text">Image Text</span>
    </div>
    <div class="spacer"></div>
    <div class="sponsorSlide" id="imgageSlide_2">
        <img alt="" src="<?php echo $this->baseUrl(); ?>/img/Sponsors/img_2.jpg" /><br />
        <span class="thumbnail-text">Image Text</span>
    </div> ... </div></div>

I am trying to first shuffle "sponsorSlide" divs and then select 7 of them randomly. I want to show it with fadeIn and fadeOut. Generally, I'm trying this code:

$('#sponsorsContent').addClass('horizontal');
$('#sponsorsContent div').addClass('hidden').removeClass('visible');
$('#sponsorsContent .sponsorSlide').rand(7).removeClass('hidden');
setInterval(function(){
    $('#sponsorsContent').fadeOut(500);
    $('#sponsorsContent .sponsorSlide').delay(500).addClass('hidden').removeClass('visible').shuffle();
    $('#sponsorsContent .sponsorSlide').rand(7).removeClass('hidden').addClass('visible');                  
    $('#sponsorsContent').fadeIn(1500);
}, 5000);

The main problem is, when the code runs, exactly before the div fades out, you can see that the images are changing! But I want to randomize them when they are not visible! I used different ways:

  • Delay in different positions and different parts
  • Use addClass("bla", 500)!
  • Show(500)/Hide(500)
  • fadeIn/fadeOut the "sponsorSlide"

FYI, I am trying to have this concept:

1- showing some random images 2- fade out 3- shuffle images 4- Select 7 random divs 5- fade in 6- go 2

Does anybody have any idea that what is my main mistake? Am I doing something in a wrong way? I got confused and I really want to find out, which way I should try and how I can manage it to work as the way I want?

ps: It does work without fadings! But I need to fade them in and out.

like image 862
Dijam Avatar asked Jul 20 '11 07:07

Dijam


People also ask

How do I completely hide a div?

The hidden attribute hides the <div> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <div> element is not visible, but it maintains its position on the page.

How do I hide a div but keep the space?

The visibility: hidden rule, on the other hand, hides an element, but the element will still take up space on the web page. If you want to hide an element but keep that element's space on the web page, using the visibility: hidden; rule is best.

How do you show a div for 10 seconds and then hide it?

Select the div element. Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.

How do I hide div on small screen?

To hide an element in a responsive layout, we need to use the CSS display property set to its "none" value along with the @media rule. The content of the second <p> element having a "hidden-mobile" class will be hidden on devices smaller than 767px.


1 Answers

You should be using a callback function in order to prevent the change from showing. If you want to hide -> change -> show, you should do it like this:

$('#image_div').fadeOut(500, function() { //first fade out!
    $('#image_div img').attr('src', 'image.jpg'); //only then change the picture
    $('#image_div').fadeIn(500);   //and fade in
});

What comes in the function(){//code} will happen ONLY when the fadeOut() is finished.

like image 54
MeLight Avatar answered Oct 26 '22 19:10

MeLight