javascript - z-index 不适用于 jquery 中的悬停宽度效果

标签 javascript jquery css ruby-on-rails z-index

我正在创建一个 ruby​​ on rails 应用程序,我有五个图像并排排成一行......我希望当我将鼠标悬停在图像上时图像的宽度增加......现在宽度正在工作但是问题是当我将鼠标悬停在任何图像上时,最后一张图像会转到另一边。我尝试了很多方法来添加 z-index 但徒劳无功...... 尝试从脚本和具有相对位置的 css 添加 z-index 以及更多方法,但现在仍在工作 这是我的 home.html.erb:

<div class="container-fluid" id="width-r">
  <%= image_tag("wood1.jpg",class: "this h-h")  %>
  <%= image_tag("wood2.jpg",class: "this")  %>
  <%= image_tag("wood3.jpg",class: "this")  %>
  <%= image_tag("wood4.jpg",class: "this")  %>
  <%= image_tag("wood5.jpg",class: "this")  %>
</div>
<div class="jumbotron">
  <h1 align="center"> Here comes the ads</h1>
</div>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript"> 
  $(document).ready(function(){
    $(".this").mouseover(function(){
      $(this).animate({width: "25%"},"fast");
     });
     $(".this").mouseout(function(){
       $(this).animate({width: "20%"},"fast");
     });
   });
</script>

这是我的 css/scss 文件:

@import "bootstrap-sprockets";
@import "bootstrap";

.navbar {
  background-color: #fff;
}

body {
  height: 100%;
}
.this {
  width: 20%;
  float: left;
  height: 581.55px;
}



.navbar-ma {
  /* remove space between the navbar and the images below */
  margin-bottom: 0px;
}  

.jumbotron {
  height: 220px;
  margin-bottom: 0px;
}

.container-fluid {
  padding: 0px;
}

最佳答案

z-index 仅适用于具有 position 样式的元素,即 position: relative; position: absolute;

添加position: relative;.this

.this {
  width: 20%;
  float: left;
  height: 581.55px;
  position: relative;
  z-index: 0;
  transition: 0.25s;
}
.this:hover{
  width: 25%;
  z-index: 1;
}

您不需要为此使用 JS。当您将鼠标悬停在 html 对象上时,:hover 起作用(transition: 0.25s; 在 CSS 中设置动画)

关于javascript - z-index 不适用于 jquery 中的悬停宽度效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38649428/

相关文章:

javascript - 为什么不能在 <Image source={}/> 中使用字符串变量? ( native react )

javascript - 如何生成仅具有最大值的 Ractive <select>

javascript - 未捕获的类型错误 : Cannot read property 'startAngle'

php - Laravel 使用ajax get请求清除 session 数据

jquery - 从以前的 div 标签中删除 CSS 类

javascript - 检查 jQuery 中的类是否存在 DIV 元素

javascript - 如何在 ckeditor 3 中对齐图像中心?

javascript - HTML 元素大小(以字节为单位)

javascript - D3.js 添加到多线图路径并更新轴

html - 为什么 Django forms.TextField 占用这么多空间,即使文本输入区域很小?