简体   繁体   English

在 div 内移动光标图像

[英]moving a cursor image inside a div

I started to develop a sniper game but i am finding a trouble moving the sniper image inside the div my main goal is when the mouse is over the div i want to change it's image to the sniper image that's what i did :我开始开发狙击手游戏,但我发现在 div 内移动狙击手图像时遇到了麻烦,我的主要目标是当鼠标悬停在 div 上时,我想将其图像更改为狙击手图像,这就是我所做的:

<center>
    <div id="field" class="cursor1">
        &nbsp;
    </div>
</center>

<script type="text/javascript">
    $(document).ready(function() {
        $("#field").mouseover(function() {
            $(this).addClass("cursor");

        });
    });
</script>
<style type="text/css">
    #field
    {
        width:300px;
        height:300px;
        border:1px solid;
    }
    .cursor
    {
        cursor:url("sniper.jpg") ;
    }
</style>

but that didn't work.但这没有用。 How can I move the image inside the div?如何在div内移动图像?

在此处输入图片说明

I would not suggest setting your mouse image with css, because it's easier to adjust image size and center it with replacing mouse cursor with a real image.我不建议用 css 设置你的鼠标图像,因为用真实图像替换鼠标光标更容易调整图像大小并将其居中。

$('#box').mouseenter(function(){ 
    $('img').css('display','block');
});

$('#box').mouseleave(function(){ 
    $('img').css('display','none');
});

$("#box").mousemove(function(event) {
    $('img').css('left',event.pageX-20);
    $('img').css('top',event.pageY-20);
});

I made a simple example of this http://jsfiddle.net/N9pu4/我做了一个简单的例子http://jsfiddle.net/N9pu4/

Also consider using canvas element for rendering graphics.还可以考虑使用 canvas 元素来渲染图形。

The basic (CSS 2.1) syntax for this property is:此属性的基本 (CSS 2.1) 语法是:

cursor:  [<url>,]* keyword

This means that zero or more URLs may be specified (comma-separated), which must be followed by one of the keywords defined in the CSS specification, such as auto or pointer.这意味着可以指定零个或多个 URL(以逗号分隔),其后必须跟随 CSS 规范中定义的关键字之一,例如 auto 或 pointer。

(see https://developer.mozilla.org/en-US/docs/Web/CSS/cursor/url?redirectlocale=en-US&redirectslug=CSS%2Fcursor%2Furl ) (参见https://developer.mozilla.org/en-US/docs/Web/CSS/cursor/url?redirectlocale=en-US&redirectslug=CSS%2Fcursor%2Furl

So you should provide a fallback value:所以你应该提供一个回退值:

.cursor
{
    cursor:url("sniper.jpg"), auto;
}
#field:hover
{ 
cursor:url("sniper.jpg") ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM