jquery - 使元素在悬停时上下移动

标签 jquery html css

我在一个网站上有三个栏目,所有三个栏目都有标题和相关的文本段落。有一个灰色框用作文本的背景,但我需要灰色框向上滑动并覆盖段落文本和列标题。

这是 HTML:

<div class="row">
      <div class="span3 lower-columns" id="lower-col-left" style="padding-top:10px;">
        Rewards
        <p class="lower-col-text">placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text</p>
        </div>
      <div class="span3 lower-columns" id="lower-col-mid">
        Schedule an Installation
        <p class="lower-col-text" style="padding-top:30px;">placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text</p>
        </div>
      <div class="span3 lower-columns" id="lower-col-right" style="padding-top:10px;">
        Contact Us
        <p class="lower-col-text">placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text placeholder text</p>
        </div>

     </div>

这是 CSS:

.span3.lower-columns {
    height: 370px;
    text-align:center;
    width: 280px;
    margin-top: 40px;
    font-size: 28px;
    background-image: url('/img/lower-col-bg2.png');
    background-repeat: no-repeat;
    background-position-y: 70px;
    background-size: 290px 290px;

这是它的样子(非悬停): enter image description here

这是背景图片:

enter image description here

我宁愿只使用 CSS,但不反对使用 jQuery。

谢谢!!!!

编辑:我自己想出来了,但它不会让我回答我自己的问题。如果有人想知道,这里是 CSS:

.span3.lower-columns {
  height: 370px;
  text-align:center;
  width: 280px;
  margin-top: 40px;
  font-size: 28px;
  background-image: url('/img/lower-col-bg2.png');
  background-repeat: no-repeat;
  background-position-y: 70px;
  background-size: 290px 290px;
  position: relative;
}
 .span3.lower-columns:hover {
  background-position-y: 0px;
  transition: .65s;
}

最佳答案

只需使用 :hover。

.span3.lower-columns:hover {
  background-image: url('/img/lower-col-bg2.png');
  background-repeat: no-repeat;
  background-position-y: 0px;
}

当然,这是假设您希望它位于标题下方,而不是您希望用灰色框隐藏文本和标题。如果你这样做(想用灰色框隐藏文本),你可以使用一个单独的带有 z-index 属性的 img 元素或一个大小和颜色正确的伪元素(带有 :after)。

关于jquery - 使元素在悬停时上下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17791351/

相关文章:

javascript - 具有多个类的元素的 jQuery 选择器不起作用

Javascript:比较表中的值

JQueryUI 对话框 maxWidth 不足

c# - 3 灵活的父 div 内的灵活 div

html - jQuery Masonry gutterWidth 和 CSS margin-right 问题

javascript - 将内联 jquery datepicker 与输入字段结合起来?

javascript - 为什么 Prop 禁用会导致 rails 形式的按钮不触发?

javascript - Jquery Colorbox 与 Firefox 的问题

javascript - 使用 html 按钮更改 json

PHP session 用 PHPSESSID 覆盖任何 cookie 和 CSS,而不调用 session_start()