Fade in on mouse movement

2020-02-24 02:35发布

How do I fade in div content on first mouse movement, like on google.com, using JavaScript? I don't want it to fade out again.

5条回答
可以哭但决不认输i
2楼-- · 2020-02-24 02:46

Using CSS3 animations, for whoever supports it at this point.

body {
    opacity: 0;
    -webkit-transition: opacity 0.3s linear;
}

In the above example, whenever we change the opacity on the body, it will do a fade effect lasting 0.3 seconds linearly. Attach it to mousemove for one time only.

document.body.onmousemove = function() {
    this.style.opacity = 1;
    this.onmousemove = null;
};

See google.com revamped here :) Chrome and Safari only.

查看更多
太酷不给撩
3楼-- · 2020-02-24 02:58

If you use jquery and want it to fade in like google you could do something like this

$('body').mousemove(function() {
  $('#content').fadeIn();
});
查看更多
贪生不怕死
4楼-- · 2020-02-24 03:01

You can create a fade in effect for the body just like Google with the following code. Please take into consideration this will have similar functionality to Google. You can apply this technique to any element with the proper event handler.

var fps = 24;
var mpf = 1000 / fps;

function fadeIn(ele, mils) {
    // ele: id of document to change.
    // mils: number of mils for the tansition.
    var whole = 0;
    var milsCount = 0;
    var subRatio = 1 / (mils / mpf);

    while (milsCount <= mils) {
        setTimeout('setOpacity("' + ele + '", ' + whole + ')', milsCount);
        whole += subRatio;
        milsCount += mpf;
    }

    // removes the event handler.
    document.getElementById(ele).onmouseover = "";
}

function setOpacity(ele, value) {
    ele = document.getElementById(ele);

    // Set both accepted values. They will ignore the one they do not need.
    ele.style.opacity = value;
    ele.style.filter = "alpha(opacity=" + (value * 100) + ")";
}

You will want to add the event handler to the body of the document in whatever fashion you normally do. Be sure to modify the fadeIn function to pull information from the target/srcElement if you decide to use an attachment method that does not accept arguments. Or you can hard code desired values and objects into the function:

Inline:

<body id="theBody" onmouseover="fadeIn('theBody', 1500)">

DOM Level 0:

document.getElementByTagName("body")[0].onmouseover = function(){ code here };
document.getElementByTagName("body")[0].onmouseover = fadeIn;

DOM Level 2:

document.getElementByTagName("body")[0].addEventListener("mouseover", fadeIn);
document.getElementByTagName("body")[0].attachEvent('onclick', fadeIn);

You will also want to set up a css rule for the body element to make sure that it is not visible when the page loads:

body {
    opacity: 0;
    filter:alpha(opacity=0);
}

I have checked this code to work correctly on IE8, Chrome, Safari, FireFox, and Opera. Good luck.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-24 03:11

I would recommend using a javascript library such as http://jquery.com/.

Using jquery, if you wanted to fade in a div with id "content", you could write the following javascript code...

$(document).mousemove(function() {
    $("#content").fadeIn();
});
查看更多
混吃等死
6楼-- · 2020-02-24 03:13

Code: (See it in action)

// attach event handler
document.body.onmousemove = function(){
  fadeIn( this, 1000 );      // 1000ms -> 1s
  this.onmousemove = null; // remove to only fade in once!
};

// sets the opacity of an element (x-browser)
function setOpacity( obj, value ) {
  if ( obj ) {
    obj.style.opacity = value / 100;
    obj.style.filter  = 'alpha(opacity=' + value + ')';
    obj.style.zoom    = 1;
  }
}

// makes an element to fade in
function fadeIn( dom, interval, delay ) {

      interval  = interval || 1000;
      delay     = delay    || 10;

  var opacity   = 0,
      start     = Number(new Date()),
      op_per_ms =  100 / interval;

  if ( typeof dom === "string" ) {
    dom = document.getElementById( dom );
  }

  function step() {

    var now     = Number(new Date()),
        elapsed = now - start;
        opacity = elapsed * op_per_ms;

    setOpacity( dom, opacity );

    if ( elapsed < interval )
      setTimeout( step, delay );
    else
      setOpacity( dom, 100 );
  }

  setTimeout( step, delay );
};

Note: the fade function could've been smaller, but in this form you can reuse it easily for any element and duration. Have fun!

查看更多
登录 后发表回答