【问题标题】:How to add a diffrent background image to different div elements on hover?如何在悬停时向不同的 div 元素添加不同的背景图像?
【发布时间】:2017-05-07 05:17:07
【问题描述】:

我有几个 div 元素在网格中对齐。当用户将鼠标悬停在它们上方时,我希望它们具有特定的不同背景图像,当鼠标离开 div 时它们会消失。基本上它是一个用户信息页面,每个方格都有一个用户的数据。当人们将鼠标悬停在方块上时,用户的图像会显示为背景图像。这是 HTML:

<div class="contributor">
  Some great stuff here
</div>

这是我目前正在使用的 jQuery 代码。问题:它为每个 div 分配相同的图像,而我希望为每个 div 分配不同的图像。

$(document).ready(function(){
  $('.contributor').mouseenter(function(){
      $(this).addClass('visible');
  });
  $('.contributor').mouseleave(function(){
    $(this).removeClass('visible');
});

});

这是“可见”类的代码:

.visible{
  background-image: url('../res/dev-1.png');
}

【问题讨论】:

  • 使用$(this).css('background-image','../res/dev-1.png'),以便您可以根据上下文更改图像...
  • 问题是您正在为visible 类静态设置图像。然后当用户悬停一个 div 时,它总是显示visible 背景图像。

标签: javascript jquery html css


【解决方案1】:

你为什么使用 jQuery?只使用 CSS 和伪类 :hover

.contributor:hover{
  background-image: url('../res/dev-1.png');
}

申请不同的div:

.contributor.diff-div:hover{
  background-image: url('../res/dev-2.png');
}

如果您可以在 CSS 中做某事,那么它几乎总是比使用 JavaScript

更好的解决方案

【讨论】:

  • 如何将不同的图像应用到不同的 div?
  • 将 ID 添加到 2 个 div 并使用 css 定位它们。每个 div 的内部 css 只需使用 background: url(../img/somepic.jpg); 请记住:ID 始终应该是唯一的!
  • @AdityaJGupta 添加辅助类,我更新了
  • @Whitcik 实际上有数百个用户数据。我将不得不创建数百条规则来为所有这些添加图像。基于用户名添加图像的任何想法,因为图像名称和用户名是相同的?
【解决方案2】:

首先,将图片url保存在名为“imageurl”的属性中(浏览器不解析):

<div class="contributor" imgageurl="../res/dev-1.png"></div>
<div class="contributor" imgageurl="../res/dev-2.png"></div>

然后,使用这个 jQuery 代码:

$('.contributor').mouseenter(function(){
      var imgageUrl = $(this).attr("imageurl");
      $(this).css("background-image" , imgageUrl); 
  });
  $('.contributor').mouseleave(function(){
    $(this).css("background-image" , "none");
  });

当然,您还必须动态构建 HTML。

【讨论】:

  • 不好,这个效果应该用纯CSS。如果您需要 HTML 中的图像 URL,您应该添加内联样式 background-image 并使用伪类 :hover 更改可见性 `background-image'
  • @Whitcik 首先它很好,因为它有效。其次,他不能使用纯 CSS,因为数据是动态的。您需要从服务器端动态创建 CSS 文件才能执行您的解决方案。最后,他要求一个 jQuery 解决方案。你可以争辩说用 jQuery 一点都不好,他最好用 Angular 或其他框架。
  • @Alon 你可以做到,感谢内联样式 :) Jquery 还可以,但是当你可以使用 CSS 获得相同的效果时,它总是会更好:) 搜索更好的解决方案,让我们变得越来越好更好:)
  • @Whitcik 不这么认为。我也会试试的。谢谢。
  • @Whitcik 再次强调,CSS 只要是静态的就更好。我宁愿拥有静态 JS 代码,也不愿拥有必须从服务器端创建的动态 CSS 文件。
【解决方案3】:

也许你可以考虑css函数而不是addClass。

$(document).ready(function(){
  $('.contributor').mouseenter(function(){
      if(this.id == "one")
         $(this).css("background-image" , "url('../res/dev-1.png')");
      else if(this.id == "two")
         $(this).css("background-image" , "url('../res/dev-2.png')");
  });
  $('.contributor').mouseleave(function(){
    $(this).css("background-image" , "none");
  });
});

【讨论】:

  • 问题在于数据的大小。实际上有数百个用户的数据,为所有用户创建一个 if-else 案例是不可行的。
【解决方案4】:

用 jquery 试试这个。假设您有从dev-1.pngdev-6.png 的六张图像,那么它将分别将它们设置为 1 到 6 号 div 的背景

$(document).ready(function(){
  $('.contributor').mouseover(function(){
     var index = $("div").index(this);
     index++;
     var bI= "background-image:url('../res/dev-"+index+".png');";
     $("this").eq(index).attr('style',bI);
  });
  $('.contributor').mouseleave(function(){
     var index = $("div").index(this);
     index++;
     var bI= "background-image:none";
     $("this").eq(index).attr('style',bI);
});

});

【讨论】:

    【解决方案5】:

    供参考(太麻烦,如果你有很多元素就不适用)

    这可以通过纯 CSS 来实现。

    图像可能需要一秒钟才能加载(随机图像服务)

    工作示例:非响应式在整页中打开

    .optionlist li {
      background: #111;
      color: #999;
      padding: .5em;
      list-style-type: none;
      border: 1px solid;
      max-width: 70px
    }
    .optionlist li:hover {
      background: red;
    }
    .optionlist li:hover:after {
      content: '';
      width: 800px;
      height: 600px;
      position: fixed;
      top: 30%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    /* Start Background Control */
    
    #red:after {
      background: url(http://lorempixel.com/800/600/)
    }
    #blue:after {
      background: url(http://lorempixel.com/800/601/)
    }
    #green:after {
      background: url(http://lorempixel.com/801/600/)
    }
    #yellow:after {
      background: url(http://lorempixel.com/801/601/)
    }
    #orange:after {
      background: url(http://lorempixel.com/799/600/)
    }
    #white:after {
      background: url(http://lorempixel.com/800/599/)
    }
    #black:after {
      background: url(http://lorempixel.com/801/599/)
    }
    /* End Background Control */
    <div class="optionlist">
      <ul>
      <li id="red">Red</li>
      <li id="blue">Blue</li>
      <li id="green">Green</li>
      <li id="yellow">Yellow</li>
      <li id="orange">Orange</li>
      <li id="white">White</li>
      <li id="black">Black</li>
        </ul>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多