简体   繁体   English

鼠标悬停更改图像

[英]Image Change with Mouse Hover

I have some links to my facebook and twitter, these links are images. 我有一些指向我的Facebook和Twitter的链接,这些链接是图像。 I want these links to turn lighter when I hover over them. 将鼠标悬停在这些链接上时,我希望它们变得更轻。 I was thinking I could do this by making two images and making the images change when I hover over the image link. 我当时想可以通过制作两个图像并在将鼠标悬停在图像链接上时更改图像来实现此目的。 Is this the best way to do it and if it is how do i do it? 这是最好的方法吗,如果是我该怎么做? I couldn't find any help on how to do it this way. 我找不到有关如何以这种方式进行操作的任何帮助。

Here is my HTML: 这是我的HTML:

<div class="social">

 <a href="https://www.facebook.com/seth.urquhart?sk=wall&v=wall">
  <img src="../img/facebook_logo_extended.jpg"/>
 </a>

</div>

<br>

<div class="social">

 <a href="https://twitter.com/SethUrquhart">
  <img src="../img/twitter_logo_extended.jpg"/>
 </a>

</div>

Here is my CSS: 这是我的CSS:

p {
color: #232323;
text-indent:0px;
margin-left:30px;
padding-right: 30px;
}

ul  {
text-align: center;
color: gray;
}

ul a {
text-decoration: none;
color: black;
}

ul a:hover {
text-decoration: underline;
    margin-right: auto;
margin-left: auto;
}

html {
background: #e8e9e1;
}

h1 {
text-align: center;
}

body {
font-family: sans-serif;
color: #232323;
}

.wrap {
min-width: 600px;
width: 1200px;
margin: auto;
height: 100px;
text-align: center;
background-color: none;
}

.content {
background: #ffffff;
width: 900px;
margin-left: auto; 
margin-right:auto; 
height: auto;
text-indent: 50px;
}

.footer {
text-align: center;
background-color: #383838;
width: 900px;
margin-left: auto;
margin-right: auto;
color: #e8e9e1;
}

.social {
width: 900px;
margin: auto;
height: 100px;
text-align: center;
background-color: none;
}

.social:hover {
margin-right: auto;
margin-left: auto;
background:#cccccc;
color:#000;
}

ul#list-nav {
padding: 0;
list-style: none;
width: 605px; 
margin: 0 auto;
}

ul#list-nav li {
display:inline;
}

ul#list-nav li a {
text-decoration:none;
padding:5px 0;
width:150px;
background:#383838;
color:#eee;
float:left;
border-left:1px solid #fff;
}

ul#list-nav li a:hover {
margin-right: auto;
margin-left: auto;
background:#cccccc;
color:#000;
}

Assuming you're willing to use CSS3, I created an example showing one way to get a brief widening effect for the icons (I suppose that is what "dense" means in the question). 假设您愿意使用CSS3,我创建了一个示例,展示了一种获得图标短暂扩展效果的方法(我想这就是“密集”在问题中的含义)。 Reduced code here: 减少代码在这里:

.icon {
  -webkit-transition: 0.25s;
  transition: 0.25s;
}

.icon:hover {
  position: relative;
  z-index: 1;
  transform: scale(1.7);
  -ms-transform: scale(1.7); /* IE 9 */
  -webkit-transform: scale(1.7); /* Safari and Chrome */
}

The transform property has good support. transform属性具有良好的支持。 The effect with transition isn't so well supported (no IE9 support), but if you are thinking on graceful degration, I think it's quite valid to use that. transition效果没有得到很好的支持(没有IE9支持),但是如果您正在考虑优雅的降级,我认为使用它是相当有效的。

EDIT 编辑

I'm updating this answer because it could help other people in future. 我正在更新此答案,因为它可能会在将来帮助其他人。 The accepted answer isn't the right approach , since it's using obtrusive JavaScript to do things about styling, where CSS is the right tool. 公认的答案不是正确的方法 ,因为它使用过分的JavaScript来做有关样式的事情,而CSS是正确的工具。 I really hope the OP will take a look here and change their code. 我真的希望OP可以在这里查看并更改其代码。

Based on the OP's feedback, I updated the example showing how to get a brightness effect simulated by changing the opacity property with a fallback using filter for IE6-8. 根据OP的反馈,我更新了该示例该示例显示了如何通过使用IE6-8的filter使用后备功能更改opacity属性来获得模拟的亮度效果。 In short, here's the code: 简而言之,下面是代码:

.icon {
  opacity: 1;
  filter: Alpha(Opacity=100);
}

.icon:hover {
  opacity: .6;
  filter: Alpha(Opacity=60);
}

It's easy and works very well when the parent's background-color is lighter than the element. 当父级的background-color比元素浅时,这很容易并且效果很好。 If you need something more elaborated (if you really want changing between two images), I really suggests you to use CSS sprites . 如果您需要更详细的说明(如果您真的想在两个图像之间进行更改),我真的建议您使用CSS sprites

I don't know what you mean by dense, but you can alter any image property via the onmouseover and restore it with onmouseout. 我不知道稠密是什么意思,但是您可以通过onmouseover更改任何图像属性,并使用onmouseout恢复它。 Here's a code snippet to show how to do it. 这是一个代码片段,展示了如何做到这一点。 This code simply makes an image dimmer when the mouse is over it, then restores it when the mouse leaves: 这段代码只是在鼠标悬停在其上时使图像变暗,然后在鼠标离开时将其恢复:

<img 
    src = "test.jpg" 
    style = "width:50%;" 
    id = "test" 
    onmouseover = "document.getElementById('test').style.opacity=0.5"
    onmouseout = "document.getElementById('test').style.opacity=1" />

If you wanted to make the images bigger on the hover, you'd change any of the size attributes. 如果要在悬停时放大图像,则可以更改任何大小属性。 For instance, here's a particularly dramatic size jump: 例如,这是一个特别引人注目的尺寸变化:

<img 
    src = "test.jpg" 
    style = "width:50%;" 
    id = "test" 
    onmouseover = "document.getElementById('test').style.width='75%'"
    onmouseout = "document.getElementById('test').style.width='50%'" />

Please note that the above is for illustrative purposes only. 请注意,以上内容仅用于说明目的。 There are other ways of doing this, and I am not saying the way I presented is the best or even a good one. 还有其他方法可以做到这一点,我并不是说我提出的方法是最好的,甚至是好的方法。 However, it's clear and I just want you to clearly see how this can be done. 但是,很明显,我只想让您清楚地知道如何做到这一点。

The simpliest solution would probably for you to use background-images rather than images so you can just switch between them. 最简单的解决方案可能是让您使用背景图像而不是图像,因此您可以在它们之间进行切换。 You can even go as far as creating 3 states this way.. inactive, hover, and selected.. 您甚至可以通过这种方式创建3个状态。.inactive,悬停和selected ..

Consider cascades and specificity.. If you define your inactive state first, hover state is defined second overwriting the same definitions, selected state is defined last, again with the same definitions and level of specificity. 请考虑级联和特定性。如果首先定义不活动状态,则先定义悬停状态,然后覆盖相同的定义,最后定义选定的状态,再次使用相同的定义和特定性级别。 Now each will overwrite the other in the appropriate or they will happen. 现在,每一个都将在适当的情况下覆盖另一个,否则它们将发生。

An image 一个图像

div { background:url('http://www.placehold.it/200x200/f2f2f2') no-repeat; }

On hover display a different image 悬停时显示其他图像

div:hover { background:url('http://www.placehold.it/200x200/666666') no-repeat; }

If the element is an anchor or has some onclick function defined with it.. display a different image on select with a new class 如果元素是锚点或具有定义的onclick函数..在选择带有新类的图像时显示不同的图像

div.selected { background:url('http://www.placehold.it/200x200/000000') no-repeat; }

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

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