如何使用 创建可点击的 CSS 精灵而不是 JavaScript?

发布于 2024-11-04 16:59:13 字数 738 浏览 1 评论 0原文

以下是使用 Javascript 实现的可点击 CSS 精灵的示例:

此处为实时演示。

HTML:

<div></div>

CSS:

div {
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;
}
div:hover {
    background-position: -100px -100px;
}

JS:

$(function() {
    $('div').click(function() {
        window.location = "http://google.com";
    });
});

是否可以不使用 Javascript 实现相同的效果?

(我能想到的唯一方法是使用 ,但如何使用?)

Here is an example of click-able CSS sprite implemented with Javascript:

Live demo here.

HTML:

<div></div>

CSS:

div {
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;
}
div:hover {
    background-position: -100px -100px;
}

JS:

$(function() {
    $('div').click(function() {
        window.location = "http://google.com";
    });
});

Is that possible to achieve the same without Javascript ?

(The only way I can think of is to use <a href="...">, but how ?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

囍孤女 2024-11-11 16:59:13

对于 元素,为其指定 hrefdisplay: inline-block,以及您设置的所有其他 CSS 属性。

a {
    display: inline-block;
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;
}
a:hover {
    background-position: -100px -100px;
}

工作演示:http://jsfiddle.net/t629m/13/

With an <a> element, give it a href and display: inline-block, along with all the other CSS properties you have set.

a {
    display: inline-block;
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;
}
a:hover {
    background-position: -100px -100px;
}

Working demo: http://jsfiddle.net/t629m/13/

稳稳的幸福 2024-11-11 16:59:13

您以一种令人费解的方式提出了问题,但答案很简单:

  • div 更改为 a
  • display: block 应用于 a

参见: http://jsfiddle.net/65HdK/

<a href="http://google.com/"></a>

a{
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;

    display: block
}
a:hover {
    background-position: -100px -100px;
}

You asked your question in a convoluted way, but the answer is simple:

  • Change the div to an a.
  • Apply display: block to the a.

See: http://jsfiddle.net/65HdK/

<a href="http://google.com/"></a>

a{
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;

    display: block
}
a:hover {
    background-position: -100px -100px;
}
夏有森光若流苏 2024-11-11 16:59:13

@misha,检查类似 http://jsfiddle.net/sandeep/t629m/7/

最主要的是你必须在 标签中 display:block

因为 是一个 内联元素
例子:

a {
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;
    display:block;
}

@misha,check the like http://jsfiddle.net/sandeep/t629m/7/

the main thing is that you have to display:block in <a> tag

because <a> is an inline element
example:

a {
    width: 100px;
    height: 100px;
    background-image: url(http://perfectwebtutorials.com/wp-content/uploads/2011/03/spritecss.png);
    background-position: -300px -100px;
    display:block;
}
雨的味道风的声音 2024-11-11 16:59:13
<a href="http://google.com"><div></div></a>

似乎工作正常。现场演示:http://jsfiddle.net/vuZz4/

<a href="http://google.com"><div></div></a>

Seems to be working fine. Live demo: http://jsfiddle.net/vuZz4/

双手揣兜 2024-11-11 16:59:13

Simply change it a <a> and add display:block or similar to your css.

Example here.

http://jsfiddle.net/blowsie/t629m/11/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文