如何使背景图像可点击(javascript或css)

发布于 2024-12-26 02:15:26 字数 521 浏览 5 评论 0原文

请看看这个 fiddle

如何使背景图片可点击?当点击“参加调查”时,将进入一个新页面。

<div style="height:300px;width:300px;border-style:solid;border-width:5px;background-image:url('http://cdn.survey.io/embed/1.0/right_top_blue.png'); background-repeat:no-repeat;background-position:right top;">

By default, the background-image property repeats an image both horizontally and vertically.

</div>

Javascript 或 CSS 都是首选。

编辑:我不想使用图像浮动到右角。

Please take a look this fiddle

How to make the background image clickable? When click "Take the survey" will go to a new page.

<div style="height:300px;width:300px;border-style:solid;border-width:5px;background-image:url('http://cdn.survey.io/embed/1.0/right_top_blue.png'); background-repeat:no-repeat;background-position:right top;">

By default, the background-image property repeats an image both horizontally and vertically.

</div>

Javascript or CSS are all preferred.

Edit: I don't want to use image float to right corner.

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

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

发布评论

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

评论(5

酒绊 2025-01-02 02:15:26

你不应该。

如果用户关闭了样式或者正在使用屏幕阅读器怎么办?他们也应该能够参加调查。

div{position:relative;}
a {display:block; width:120px; height:120px;
    position:absolute;
    top:0; right:0;
    text-indent:-9999px;

    background-image:url('http://cdn.survey.io/embed/1.0/right_top_blue.png'); 
    background-repeat:no-repeat;background-position:right top;}

演示

You shouldn't.

What if a user has styles turned off, or is using a screen reader? They should be able to take the survey as well.

div{position:relative;}
a {display:block; width:120px; height:120px;
    position:absolute;
    top:0; right:0;
    text-indent:-9999px;

    background-image:url('http://cdn.survey.io/embed/1.0/right_top_blue.png'); 
    background-repeat:no-repeat;background-position:right top;}

Demo

枫林﹌晚霞¤ 2025-01-02 02:15:26

这是一个使用相对位置 + 绝对位置

http://jsfiddle.net/RZWCS/

的简单示例,您也可以考虑绝对定位链接并使用该图像作为链接背景(链接的文本可以与图像上显示的文本相同,并使用负文本缩进或类似技术移到外部)

that's a simple example using position relative + absolute

http://jsfiddle.net/RZWCS/

you could also think to absolute positioning the link and use that image as link background (the text of the link could be the same appearing on the image and moved outside with negative text-indent or similar techniques)

绾颜 2025-01-02 02:15:26

请在此处查看更多信息:http:// bluedogwebservices.com/css-trick-turning-a-background-image-into-a-clickable-link/

另外,您可以使 div 相对,然后只需将图像用锚点包裹并将其绝对放置在顶部右角。

编辑

HTML

<div class="wrapper">
<a href="/"><img src="yoursrc" alt="description"/></a>
<p>Some other text regularly positioned.</p>
<div>

CSS:

.wrapper
{
position:relative;
width:500px;
height:500px;
}

.wrapper a
{
position:absolute;
top:0px;
right:0px;
}

Check here for more info: http://bluedogwebservices.com/css-trick-turning-a-background-image-into-a-clickable-link/

Also, you could make the div relative, then just put image wrapped with anchor and position it absolutely in top right corner.

Edit

Html

<div class="wrapper">
<a href="/"><img src="yoursrc" alt="description"/></a>
<p>Some other text regularly positioned.</p>
<div>

CSS:

.wrapper
{
position:relative;
width:500px;
height:500px;
}

.wrapper a
{
position:absolute;
top:0px;
right:0px;
}
如何视而不见 2025-01-02 02:15:26

我在这里可能是错的,但我不认为 CSS 背景可以绑定事件。我个人会用透明的 div 覆盖它,或者使用真实的图像来访问 DOM 元素的事件。

如果有人有办法做到这一点,我会很感兴趣:)

I could be wrong here, but I don't think that CSS backgrounds can have events bound to them. I would personally cover it with a transparent div, or use a real image to tap into the events of the DOM element.

I would be interested if someone had a way to do this :)

就此别过 2025-01-02 02:15:26

直接把div变成元素就可以了吗?

<a href="mysite.com" 
    style="display: block; 
    height:300px; 
    width:300px;
    border: 5px solid;
    background: url('http://cdn.survey.io/embed/1.0/right_top_blue.png') right top no-repeat;">
<a/>

Just turn the div into an <a> element?

<a href="mysite.com" 
    style="display: block; 
    height:300px; 
    width:300px;
    border: 5px solid;
    background: url('http://cdn.survey.io/embed/1.0/right_top_blue.png') right top no-repeat;">
<a/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文