10

Im trying to do the following: - a Link is clicked triggering a function that will .show a DIV (#page-cover) dimming down my entire background. This div has a z-index of 999 - Then I want another div (#red) to appear upon the dimmed background / fadeup/show with a higher z-index

My CSS:

#page-cover {
    display: none;
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: #000;
    z-index: 999;
    top: 0;
    left: 0;
}
    #red {
    background-color: red;
    width: 100px;
    height: 100px;
}

HTML

//Triggering link
<a href="#" onclick="dimBackground("Element1")">Element 1</a>
//Div to appear upon dimmed DIV
<div id="red">hejsa</div>
//Dimming DIV
<div id="page-cover"></div>

Jquery

function dimBackground() {
    $("#page-cover").css("opacity",0.6).fadeIn(300, function () {
        //Attempting to set/make #red appear upon the dimmed DIV
        $('#red').css('zIndex', 10000);
    });
}

page-cover fades up as supposed, however i've not had any success making #red appear afterwards.

Any suggestions?

1
  • have you found any solution for this?
    – anam
    May 15, 2014 at 7:15

1 Answer 1

11

CSS

#page-cover {
    display: none;
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: #000;
    z-index: 999;
    top: 0;
    left: 0;
}
    #red {
    background-color: red;
    width: 100px;
    height: 100px;
}

HTML:

//Triggering link
<a id="triggeringlink" href="#">Element 1</a>
//Div to appear upon dimmed DIV
<div id="red">hejsa</div>
//Dimming DIV
<div id="page-cover"></div>

JS

$(window).load(function(e){
  $('#triggeringlink').on('click',function(e){
     $("#page-cover").css("opacity",0.6).fadeIn(300, function () {            
        $('#red').css({'position':'absolute','z-index':9999});
     });
   e.preventDefault();
   });
});

This is a slightly different approach, but I think it's what you're after. Instead of inline JS, we're binding a click event to the <a> tag when the page loads. I've updated the CSS so that the RED div is hidden, and positioned absolutely - above the overlay. You may need to pull screen dimensions and position it centered right before fading it in, let me know if this is your intent and I'll update the answer.

4
  • Hi Aaron, Thanks a lot for taking the time to look at my question, ill just try out your example shortly. However there's one important note here - my #red div needs to be visible at all time. What im basically looking to achieve is a somewhat highlighting feature, so imagine that I have 4 elements. One is clicked > All page dims down > and the clicked object fades up and becomes the ony visible-non-dimmed-down object. Feb 16, 2013 at 19:35
  • Is your #red is relatively positioned?
    – Aaron
    Feb 16, 2013 at 19:36
  • I've updated the answer, but there will need to be another part to this. When closing the overlay and returning to your site - you need to reset the #red positioning to relative. Another alternative is to use .clone() and drop it inside of the overlay element, then delete it when closing the overlay.
    – Aaron
    Feb 16, 2013 at 19:41
  • 3
    $('#red').css({'position':'absolute','z-index':9999}); Just beware of the typo in the JS part of the answer. ("aboslute" which should be "absolute") Jul 29, 2014 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.