如何缩短此代码?

时间:2017-01-13 18:45:07

标签: javascript jquery function variables mouseenter

我试图为每个元素创建on() mouseenter函数,但有什么方法可以某种方式缩短该函数。问题是我已经多次在mouseenter函数上创建了它。请帮帮我们:)

这是下面的代码



var $member1 = $('.team-content img:nth-child(1)'),
  $member2 = $('.team-content img:nth-child(2)'),
  $member3 = $('.team-content img:nth-child(3)'),
  $member4 = $('.team-content img:nth-child(4)')

$(".member1").on('mouseenter', function() {
  $member1.css({
    "left": "0px"
  });
}).on('mouseleave', function() {
  $member1.css({
    "left": ""
  });
});

$(".member2").on('mouseenter', function() {
  $member2.css({
    "left": "0px"
  });
}).on('mouseleave', function() {
  $member2.css({
    "left": ""
  });
});

$(".member3").on('mouseenter', function() {
  $member3.css({
    "left": "0px"
  });
}).on('mouseleave', function() {
  $member3.css({
    "left": ""
  });
});

$(".member4").on('mouseenter', function() {
  $member4.css({
    "left": "0px"
  });
}).on('mouseleave', function() {
  $member4.css({
    "left": ""
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="team" class="about-team">
  <div class="team-header">
    <h2 class="team-text">Our Team</h2>
    <div class="divider"></div>
  </div>
  <div class="section-content">
    <div class="row text-center">
      <div class="col-xs-6 col-md-3 col-lg-3 member1">
        <h2 class="t-seperator">John Doe</h2>
        <span>/CEO</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member2">
        <h2 class="t-seperator">Jesica Ice</h2>
        <span>/DESIGNER</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member4">
        <h2 class="t-seperator">Anna Moon</h2>
        <span>/MARKETER</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member3">
        <h2 class="t-seperator">Michael Huge</h2>
        <span>/DEVELOPER</span>
      </div>
    </div>
  </div>
</div>

<div id="main-team" class="team-content">

  <img src="assets/img/team/team1.jpeg" alt="Team 1">
  <img src="assets/img/team/team2.jpg" alt="Team 2">
  <img src="assets/img/team/team3.jpg" alt="Team 3">
  <img src="assets/img/team/team4.jpg" alt="Team 4">

</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

如果使用index()函数检测到悬停的.member*的索引,则可以添加常规处理程序并在相应的img元素上应用该函数。以下是一个示例:(在示例中,我为了清晰起见而更改颜色

&#13;
&#13;
$(".team-content img").on('mouseenter', function(e) {
  var imageIndex = $(".team-content img").index(e.target) + 1;
  $(".member" + imageIndex).css({
    "color": "red"
  });
}).on('mouseleave', function(e) {
  var imageIndex = $(".team-content img").index(e.target) + 1;
  $(".member" + imageIndex).css({
    "color": "black"
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="team" class="about-team">
  <div class="team-header">
    <h2 class="team-text">Our Team</h2>
    <div class="divider"></div>
  </div>
  <div class="section-content">
    <div class="row text-center">
      <div class="col-xs-6 col-md-3 col-lg-3 member1">
        <h2 class="t-seperator">John Doe</h2>
        <span>/CEO</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member2">
        <h2 class="t-seperator">Jesica Ice</h2>
        <span>/DESIGNER</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member4">
        <h2 class="t-seperator">Anna Moon</h2>
        <span>/MARKETER</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member3">
        <h2 class="t-seperator">Michael Huge</h2>
        <span>/DEVELOPER</span>
      </div>
    </div>
  </div>
</div>

<div id="main-team" class="team-content">
  <img src="assets/img/team/team1.jpeg" alt="Team 1">
  <img src="assets/img/team/team2.jpg" alt="Team 2">
  <img src="assets/img/team/team3.jpg" alt="Team 3">
  <img src="assets/img/team/team4.jpg" alt="Team 4">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用循环来立即完成所有操作!

for (var i = 1; i <= 4; i++) {
    $('.member' + i).on('mouseenter', function() {
        $('.team-content img:nth-child(' + i + ')').css({'left': '0px'});
    }).on('mouseleave', function() {
        $('.team-content img:nth-child(' + i + ')').css({'left': ''});
    });
}

答案 2 :(得分:0)

我可能已经过度考虑了,但如果会员编号和第n个孩子编号相同,为什么不使用它来创建相关的访问者?我在这里有三个功能:第一个在初始化时运行,并将成员号的整数部分保存为数据属性,以便以后检索。 mouseenter和mouseleave函数检索保存的成员编号,并使用该编号构建选择器。

&#13;
&#13;
$("div[class*='member']").each(function() {
    // for every member element, let's save its
    //  relevant nth-child number.
    var myNumber = 0;
    var myClasses = $(this).prop("class").split(" ");
   
  // check all classes to find the member number
    for (var i = 0; i <= myClasses.length; i++) {
      if ( myClasses[i].startsWith("member") ) {
        // strip out JUST the number portion.
        myNumber = myClasses[i].match(/\d+/)[0];
      }
      if( myNumber != 0 )
      break;
    }
    // Save the number portion for later.
    $(this).data("nthNumber", myNumber);
  }).on('mouseenter', function() {
    // retrieve the saved number
    var selector = ".team-content img:nth-child("+ $(this).data("nthNumber") +" )"; 
    $(selector).show();
  }).on('mouseleave', function() {
    // retrieve the saved number
    var selector = ".team-content img:nth-child("+ $(this).data("nthNumber") +" )"; 
    $(selector).hide();
  });
&#13;
.about-team {
  width: 400px;
  float: left;
}
.team-content {
  position: absolute;
  right: 5px;
  top: 5px;
}
.team-content img {
  display: none;
  border: 1px dotted red;
  width: 100px;
  height: 100px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="team" class="about-team">
  <div class="team-header">
    <h2 class="team-text">Our Team</h2>
    <div class="divider"></div>
  </div>
  <div class="section-content">
    <div class="row text-center">
      <div class="col-xs-6 col-md-3 col-lg-3 member1">
        <h2 class="t-seperator">John Doe</h2>
        <span>/CEO</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member2">
        <h2 class="t-seperator">Jesica Ice</h2>
        <span>/DESIGNER</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member4">
        <h2 class="t-seperator">Anna Moon</h2>
        <span>/MARKETER</span>
      </div>
      <div class="col-xs-6 col-md-3 col-lg-3 member3">
        <h2 class="t-seperator">Michael Huge</h2>
        <span>/DEVELOPER</span>
      </div>
    </div>
  </div>
</div>

<div id="main-team" class="team-content">

  <img src="assets/img/team/team1.jpeg" alt="Team 1">
  <img src="assets/img/team/team2.jpg" alt="Team 2">
  <img src="assets/img/team/team3.jpg" alt="Team 3">
  <img src="assets/img/team/team4.jpg" alt="Team 4">

</div>
&#13;
&#13;
&#13;

所以做了一些改动:首先,我把初始选择器弄错了。然后,我添加了一些CSS样式,以便我们可以看到发生的事情。这种方法的优点(虽然可能更长)是它的可扩展性。如果再添加30个员工,则必须为每个员工创建一个变量,并且每次都使用该变量。通过这种方法,它会自动发生。