0

Is it possible to add transition like when loading a web page like when you hover a div with transition effect. example: cubeupload.com

4

5 回答 5

4

You can do that, but not right out of the box using css3 and html5 only! what you can do, even without relying on heavier frameworks, is to apply to various element's onload events and append a css class when the onload events are firing!

lets say you make your body's css look like this :

body {
    opacity : 0;
    transition : opacity 1s ease; 
}

.loaded {
   opacity : 1;
}

you could then do something like this in javascript :

<body onload="this.classList.add('loaded')">

in some browsers the this variable within the body element is not really recognized, it's better to use document.body instead!

<body onload="document.body.classList.add('loaded')">

this gives you the freedom to play with various css properties all at once within css, transform : all 1s ease; without the hassle of learning new frameworks at all!

Here's a working example

<!doctype>
<html>
    <head>
        <style>

            body { background: orange; -webkit-transition: all 1s ease; transition: all 1s ease; }

            .loaded { background: red; }

        </style>

        <script>function loaded (el) { el.classList.add('loaded') }</script>

    </head>
    <body onload="loaded(document.body)">

        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    </body>
</html>

Notice : This is, as requested, a html5/css3 solution! older browser may not support css transitions or the classList property!

于 2013-04-29T23:44:37.083 回答
1

If you want the whole page to have a transition effect:

$(document).ready(function() {
        $("body").css("display", "none");
        $("body").fadeIn(2000);
});

Preview jsFiddle: http://jsfiddle.net/tXeks/

于 2013-04-29T23:42:16.003 回答
1

Of course.

Simply use jQuery and jQuery.blockUI plugin and use some code such as:

<script language='javascript' type='text/javascript'>
$(document).ready(function(){
    $.blockUI();
    $(window).load(function(){
        $.unblockUI();
    });
});
</script>

In this case, when the page is ready the block is displayed, and when the whole page is loaded block is simply hidden. :)

于 2013-04-29T23:42:48.747 回答
0

For example of your example, a pure CSS solution:

a:hover { color:white; }

That would make the link text color turn white on hover.

You can animate it with CSS transitions. http://www.w3schools.com/css3/css3_transitions.asp

于 2013-04-29T23:42:43.693 回答
0

Best is using plain javascript as You need to wait for jquery which often is loaded on the end of page, event that page is loaded. add this after body

 <script type="text/javascript">
  var sheet = document.createElement('style');
  sheet.innerHTML = "#main {opacity:0;}"; // your main content
  element = document.getElementById('wrapper'); // your 'main content wrapper
  element.appendChild(sheet);
  setTimeout(function(){ 
    sheet.innerHTML = "#main {opacity:1;}";
    element.appendChild(sheet); 
  }, 400); // time to wait to start transition
    </script>
  <div class="wrapper">
      <div id="main">Magic happens here</div>
 </div>
于 2015-09-18T09:27:04.307 回答