鼠标悬停显示我的光标周围的背景图像的一部分

时间:2015-06-30 20:56:36

标签: javascript jquery html5 css3

我想在我的网页上添加背景图片,隐藏它并在鼠标悬停时显示光标周围的图像部分。我想在jQuery,JavaScript,CSS3,HTML5中这样做。有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

这是一种方法(另一个延迟低于该延迟的演示):



var circle = document.getElementById('circle');

document.addEventListener('mousemove', positionCircle);

function positionCircle(e){
    circle.style.left = e.clientX + 'px';
    circle.style.top = e.clientY + 'px';
}

/* Basic styles for the demo */

html{
    min-height: 100%;
}
body{
    background-image: url(http://www.pageresource.com/wallpapers/wallpaper/cute-cat-kitty-kitten.jpg);
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    min-height: 100%;
}

/* Your circle */

#circle{
    padding: 2em;
    position: fixed;
    top: 0;
    left: 0;
    margin-top: -502em;
    margin-left: -502em;
    background: transparent;  /* Show through the circle */
    -moz-border-radius: 504em;
    border-radius: 504em;
    border: 500em solid rgba(0,0,0,.6); /* Make a huge border to hide the rest */
    opacity: 0;
    -webkit-transition: opacity .5s ease;
    -moz-transition: opacity .5s ease;
    transition: opacity .5s ease;
}

#circle:hover{
    opacity: 1;
}

<div id="circle"></div>
&#13;
&#13;
&#13;

你也可以延迟,添加视觉效果:

&#13;
&#13;
var circle = document.getElementById('circle');

document.addEventListener('mousemove', positionCircle);

function positionCircle(e){
    var event = e;
    setTimeout(function(){
        circle.style.left = event.clientX + 'px';
        circle.style.top = event.clientY + 'px';
    },70);
}
&#13;
/* Basic styles for the demo */

html{
    min-height: 100%;
}
body{
    background-image: url(https://s3.amazonaws.com/prod_sussleimg/cbb1ee8720943f710058cd92ce376958.jpg);
    background-position: 0 -5em;
    background-size: cover;
    background-repeat: no-repeat;
    min-height: 100%;
}

/* Your circle */

#circle{
    padding: 2em;
    position: fixed;
    top: 0;
    left: 0;
    margin-top: -502em;
    margin-left: -502em;
    background: transparent;  /* Show through the circle */
    -moz-border-radius: 504em;
    border-radius: 504em;
    border: 500em solid rgba(0,0,0,.6); /* Make a huge border to hide the rest */
    opacity: 0;
    -webkit-transition: opacity .5s ease;
    -moz-transition: opacity .5s ease;
    transition: opacity .5s ease;
}

#circle:hover{
    opacity: 1;
}
&#13;
<div id="circle"></div>
&#13;
&#13;
&#13;