JS查找最接近点击功能

时间:2018-05-03 04:16:37

标签: javascript jquery

我正在尝试找到与点击的.cat-dropdown-menu最接近的.title,以便一次只触发一个下拉列表。我想我需要重新配置这部分代码,以便在点击时找到最接近的.cat-dropdown-menu,但我不确定。

$(".cat-dropdown .title").click(function() {
  if ($container.height() > 0) {
    closeMenu(this);
  } else {
    openMenu(this);
  }
});

var $container = $('.cat-dropdown-menu'),
  $list = $('.cat-dropdown-menu .cat-list'),
  listItem = $list.find('.cat-item');

$(".cat-dropdown .title").click(function() {
  if ($container.height() > 0) {
    closeMenu(this);
  } else {
    openMenu(this);
  }
});

$(".cat-dropdown-menu .cat-item").click(function() {
  closeMenu(this);
});

function closeMenu(el) {
  $(el)
    .closest(".cat-dropdown")
    .toggleClass("closed")
    .find(".title")
    .text($(el).text())
    .css('color', el.style.color);
  $container.css("height", 0);
  $list.css("top", 0);
}


function openMenu(el) {
  $(el).parent().toggleClass("closed");

  $container.css({
      height: 200
    })
    .mousemove(function(e) {
      var heightDiff = $list.height() / $container.height(),
        offset = $container.offset(),
        relativeY = (e.pageY - offset.top),
        top = relativeY * heightDiff > $list.height() - $container.height() ?
        $list.height() - $container.height() : relativeY * heightDiff;

      $list.css("top", -top);
    });
}
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.cat-dropdown {
  text-align: left;
  color: #343c3f;
  border: 1px solid #a2acb0;
}

.cat-dropdown.closed .cat-dropdown-menu {
  margin-top: 0px;
}

.cat-dropdown.closed .cat-dropdown-menu .cat-item {
  height: 0px;
}

.cat-dropdown.closed .title {
  border-bottom: none;
}

.cat-dropdown.closed .title:after {
  margin-top: -16px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
}

.cat-dropdown .title {
  width: 100%;
  position: relative;
  height: 40px;
  padding: 12px;
  cursor: pointer;
  border-bottom: 1px solid #d9e1e4;
}

.cat-dropdown .title:after {
  display: block;
  content: "▾";
  position: absolute;
  right: 14px;
  margin-top: -16px;
  -webkit-transform: rotate(180deg);
  -moz-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  transform: rotate(180deg);
}

.cat-dropdown .cat-dropdown-menu {
  position: relative;
  overflow: hidden;
  max-height: 200px;
  -webkit-transition: all 0.2s;
  -moz-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-box-sizing: "border-box";
  -moz-box-sizing: "border-box";
  box-sizing: "border-box";
}

.cat-dropdown .cat-list {
  position: absolute;
  top: 0;
  width: 100%;
}

.cat-dropdown .cat-list .cat-item {
  width: 100%;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #d9e1e4;
  padding: 0 12px;
  vertical-align: top;
  overflow: hidden;
  cursor: pointer;
  -webkit-transition: margin-top 0.5s, height 0.5s;
  -moz-transition: margin-top 0.5s, height 0.5s;
  transition: margin-top 0.5s, height 0.5s;
}

.cat-dropdown .cat-list .cat-item:hover {
  background-color: #d9e1e4;
  color: #343c3f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cat-dropdown closed">
  <div class="title">
    <label>Pick 2</label></div>
  <div class="cat-dropdown-menu">
    <ul class="cat-list">
      <li class="cat-item" style="color:#3772b1">Henry Christensen</li>
      <li class="cat-item" style="color:#4eaddc">Lamar Flynn</li>
      <li class="cat-item" style="color:#ff9148">Jameson Davidson</li>
    </ul>
  </div>
</div>
<div class="cat-dropdown closed">
  <div class="title">
    <label>Pick 2</label></div>
  <div class="cat-dropdown-menu">
    <ul class="cat-list">
      <li class="cat-item" style="color:#3772b1">Henry Christensen</li>
      <li class="cat-item" style="color:#4eaddc">Lamar Flynn</li>
      <li class="cat-item" style="color:#ff9148">Jameson Davidson</li>
    </ul>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

当您执行var $container = $('.cat-dropdown-menu')$list = $('.cat-dropdown-menu .cat-list')时,您将获得页面上的所有菜单。你应该只点击与标题相关的那些。将这些变量移动到click事件的范围并将它们限制为单击的项目应解决问题。

e.g。

$(".cat-dropdown .title").click(function() {
var $menu = $(this).siblings('.cat-dropdown-menu')
  if ($menu.height() > 0) {
    closeMenu($menu);
  } else {
    openMenu($menu);
  }
});

$(".cat-dropdown-menu .cat-item").click(function() {
  var $menu = $(this).parents('.cat-dropdown-menu')
  closeMenu($menu);
  $menu
    .closest(".cat-dropdown")
    .find(".title")
    .text($(this).text())
    .css('color', this.style.color);
});

function closeMenu($menu) {
  $list = $menu.children('.cat-list');
  
  $menu
    .closest(".cat-dropdown")
    .toggleClass("closed");
  $menu.css("height", 0);
  $list.css("top", 0);
}


function openMenu($menu) {
  $list = $menu.children('.cat-list');
  
  $menu.parent().toggleClass("closed");

  $menu.css({
      height: 200
    })
    .mousemove(function(e) {
      var heightDiff = $list.height() / $menu.height(),
        offset = $menu.offset(),
        relativeY = (e.pageY - offset.top),
        top = relativeY * heightDiff > $list.height() - $menu.height() ?
        $list.height() - $menu.height() : relativeY * heightDiff;

      $list.css("top", -top);
    });
}
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.cat-dropdown {
  text-align: left;
  color: #343c3f;
  border: 1px solid #a2acb0;
}

.cat-dropdown.closed .cat-dropdown-menu {
  margin-top: 0px;
}

.cat-dropdown.closed .cat-dropdown-menu .cat-item {
  height: 0px;
}

.cat-dropdown.closed .title {
  border-bottom: none;
}

.cat-dropdown.closed .title:after {
  margin-top: -16px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
}

.cat-dropdown .title {
  width: 100%;
  position: relative;
  height: 40px;
  padding: 12px;
  cursor: pointer;
  border-bottom: 1px solid #d9e1e4;
}

.cat-dropdown .title:after {
  display: block;
  content: "▾";
  position: absolute;
  right: 14px;
  margin-top: -16px;
  -webkit-transform: rotate(180deg);
  -moz-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  transform: rotate(180deg);
}

.cat-dropdown .cat-dropdown-menu {
  position: relative;
  overflow: hidden;
  max-height: 200px;
  -webkit-transition: all 0.2s;
  -moz-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-box-sizing: "border-box";
  -moz-box-sizing: "border-box";
  box-sizing: "border-box";
}

.cat-dropdown .cat-list {
  position: absolute;
  top: 0;
  width: 100%;
}

.cat-dropdown .cat-list .cat-item {
  width: 100%;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #d9e1e4;
  padding: 0 12px;
  vertical-align: top;
  overflow: hidden;
  cursor: pointer;
  -webkit-transition: margin-top 0.5s, height 0.5s;
  -moz-transition: margin-top 0.5s, height 0.5s;
  transition: margin-top 0.5s, height 0.5s;
}

.cat-dropdown .cat-list .cat-item:hover {
  background-color: #d9e1e4;
  color: #343c3f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cat-dropdown closed">
  <div class="title">
    <label>Pick 2</label></div>
  <div class="cat-dropdown-menu">
    <ul class="cat-list">
      <li class="cat-item" style="color:#3772b1">Henry Christensen</li>
      <li class="cat-item" style="color:#4eaddc">Lamar Flynn</li>
      <li class="cat-item" style="color:#ff9148">Jameson Davidson</li>
    </ul>
  </div>
</div>
<div class="cat-dropdown closed">
  <div class="title">
    <label>Pick 2</label></div>
  <div class="cat-dropdown-menu">
    <ul class="cat-list">
      <li class="cat-item" style="color:#3772b1">Henry Christensen</li>
      <li class="cat-item" style="color:#4eaddc">Lamar Flynn</li>
      <li class="cat-item" style="color:#ff9148">Jameson Davidson</li>
    </ul>
  </div>
</div>