html - 鼠标悬停更改图像位置和大小

标签 html css hover

我正在尝试为我的网站制作一个按钮菜单,但我对鼠标悬停时图像的位置有疑问。这是我到目前为止创建的http://jsfiddle.net/tNLUx/

在鼠标悬停时,我希望选定的图像增长,而其他图像保持与第一张图像相同的位置...如何使向下的图像增长并向下移动而不是向上移动?

#btnSocial {
  width:100px;
  position: relative;
  opacity: 0.5;
  -webkit-opacity: 0.5;
  -moz-opacity: 0.5;
  transition: 0.5s ease;
  -webkit-transition: 0.5s ease;
  -moz-transition: 0.5s ease;
}
#btnSocial:hover{
  width: 150px;
  opacity: 1;
  -webkit-opacity: 1;
  -moz-opacity: 1;
}
<img src="http://img24.imageshack.us/img24/3221/32845401.png" alt="img1" id="btnSocial" class="social1" />
<img src="http://img24.imageshack.us/img24/3221/32845401.png" alt="img1" id="btnSocial" class="social2"/>
<img src="http://img24.imageshack.us/img24/3221/32845401.png" alt="img1" id="btnSocial" class="social3"/>
<img src="http://img24.imageshack.us/img24/3221/32845401.png" alt="img1" id="btnSocial" class="social4"/>

最佳答案

使用transform: scale(x, y) 缩放对象。
使用transform: translate(x, y) 移动对象。
这两个属性也可以组合:transform: scale(x, y) translate(x, y)

例子:

.btn-social {
    width: 100px;
    position: relative;
    opacity: 0.5;
    transition: 0.3s ease;
    cursor: pointer;
}

.btn-social:hover {
    opacity: 1;

    /** default is 1, scale it to 1.5 */
    transform: scale(1.5, 1.5);

    /** translate 50px from left, and 40px from top */
    /** transform: translate(50px, 40px); */

    /** combine both scale and translate */
    /** transform: scale(1.5, 1.5) translate(50px, 40px); */
}
<img src="http://sstatic.net/stackexchange/img/logos/so/so-icon.png?v=c78bd457575a" class="btn-social" />
<img src="http://sstatic.net/stackexchange/img/logos/so/so-icon.png?v=c78bd457575a" class="btn-social" /><br />
<img src="http://sstatic.net/stackexchange/img/logos/so/so-icon.png?v=c78bd457575a" class="btn-social" />
<img src="http://sstatic.net/stackexchange/img/logos/so/so-icon.png?v=c78bd457575a" class="btn-social" />

关于html - 鼠标悬停更改图像位置和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14971697/

相关文章:

php - 单击编辑按钮时,必须在表单中填写相应的详细信息以进行编辑

javascript - CKEditor:分页显示指示器

html - 使用 Chrome 鼠标悬停时 CSS 投影未完全移除

jQuery:隐藏/显示单选框上的元素更改选中和悬停

css - 左边距 : auto and margin-right: auto not working on input

javascript - 如何使菜单链接返回到索引页面并滑动到特定高度?

javascript - JQuery Mobile 检查 Div 是否展开或折叠

javascript - 按退格键后如何保留 `h1` 内容的首字母

css - 在 Bootstrap 中为自定义跨度和装订线宽度设置的值

html - 将鼠标悬停在同一父项内的不同类上时是否可以显示一个类?