将鼠标悬停在文本上时将图像更改为另一个图像

时间:2015-12-17 21:00:58

标签: javascript jquery html css

我正在尝试更改手机模拟框架内的图像。问题是图像在特定位置没有变化,即使我有代码也很难。任何人都可以帮忙吗?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <title>Mobile plugin</title>

  <!-- Bootstrap -->

  <script src="script.js"></script>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="main.css">
  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js">     </script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js">   </script>
    <![endif]-->
</head>

<body>
  <div id="wrapper" class="container">
    <div class="row-fluid">
      <div class="nexus">
        <div class="col-xs-3 col-sm-5 col-md-4">
          <img src="images/nexus.png">
          <div class="image">
            <div id="im">
              <img src="images/2a.png" id="image2">
              <img src="images/3.png" id="image3">
              <img src="images/4.png" id="image4">
            </div>
          </div>
        </div>
      </div>

      <div class="col-xs-12 col-sm-6 col-md-6">
        <div class="logofix">
          <div class="clearfix">
            <img class="logo" src="images/logo.png" width="160px">
            <ul class="image-switch">
              <li data-feature="feature-1">
                <h4>Connect</h4>
                <p>Connect the plugin using various methods.</p>
              </li>
              <li data-feature="feature-2">
                <h4>Time spend connected</h4>
                <p>This will allow you to monitor how long</p>
                <p>have been connected each day and</p>
                <p>how much data have you transferred.</p>
              </li>
              <li data-feature="feature-3">
                <h4>Browsing Habits</h4>
                <p>Shows you what percentage of your time</p>
                <p> is spent on different tasks and the amount </p>
                <p> of daily visits.</p>
              </li>
              <li data-feature="feature-4">
                <h4>Dashboard</h4>
                <p>The dashboard gives a nice and simple</p>
                <p> preview of the product functionalities,</p>
                <p> allowing you to navigate easily.</p>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3 /jquery.min.js"></script>
  <!-- Include all compiled plugins (below), or include individual files as needed -->
  <script src="js/bootstrap.min.js"></script>
</body>
</html>

这是CSS文件,用于设置电话框架和实际图像的位置:

.logofix {
   right: 60 % ;
   position: static;
   top: 30 % ;
   display: block;
 }

 #
 wrapper {
   position: relative;
   margin - right: auto;
   margin - left: auto;
   padding - top: 72 px;

 }#
 wrapper, .wrapper {
   min - height: 100 % ;
   height: auto!important;
   box - sizing: border - box;
 }

 li: hover {
   padding - left: 10 px;
   transition: all 0.2 s ease - out 0 s;
 }

 /* Settings and properties of the list text block*/
 li {
   list - style - type: none;
   font - family: Open Sans;
   font - size: 16 px;
   margin: 0 px;
   float: left;
   cursor: pointer;
   display: block;
   width: 100 % ;
   padding: 5 px 0 px;
   position: relative;
   transition: all 0.2 s ease - out 0 s;
 }
 /*END of Settings and properties of the list text block*/

 #
 image2 {

   height: 390 px;
   width: 212 px;
   position: absolute;
   top: 42 px;
   left: 23 px;
   z - index: 1;
 }

我正在使用这个简单的短javascript来改变悬停时的图像,但我不确定问题是否也不是来自它:

$(document).ready(function() {
  $('ul.image-switch li').mouseover(function(e) {
    if(e.target.nodeName.toLowerCase() == 'a') return;
    var image_src = $('a', this).data('image');
    var img = $('.image-container img');

    if(img.attr('src') != image_src) { // only do the fade if other image is selected
      img.fadeOut('slow', function() { // fadeout the current image
      img.attr('src', image_src).fadeIn(); // load and fadein new image
      });
    }
  });
});

此图片显示了当有人将鼠标悬停在右侧文字上时,网站的外观以及图片的位置 enter image description here

1 个答案:

答案 0 :(得分:0)

在这个具体案例中,我不认为你需要Js,为什么不做这样的事情呢?

.thingToHover ~ .imageToShow
{
  display : block;
}

当然,您需要先将图片设为display:none

如果您想要淡入/淡出效果,只需将transition : 0.5s添加到图片类中。

以下是JsFiddle

中的示例