Search code examples
jqueryhtmlcssfade

CSS fade after an exact date


How can I change a text/div/pic after an exact date?

For example here's a code what changes the div like an overlay. I would like that result when an exact date past.

So for example after 2018.01.01 the div automatically changes to grey. How should I code this? Tips? :)

$(document).ready(function() {
    $("#myDiv").click(function() {
        $("#overlay").show();
    });
});
#myDiv {
    width:100px;
    height:100px;
    background-color: yellow;
    margin: 50px 50px;
    padding:10px;
}

#overlay {
    display:none;
    position: absolute;    
    width:100px;
    height:100px;
    background-color: gray;
    top: 50px;
    left: 50px;
    padding: 10px;
    opacity: .8;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv">
<p>Some text</p>
<input type="button" value="A button" >
</div>

<div id="overlay"></div>

http://jsfiddle.net/QQRZc/


Solution

  • You take the current date+time, and subtract that from the target date+time, this will result in an amount of seconds until the target date+time. This answer gives a good working example for javascript, but you could also pre-calculate this serverside, e.g. with PHP.

    // Then a simple settimeout will do the rest:
    setTimeout(
        function(){ $("#overlay").show(); },
        secondsTillTarget*1000
    );