首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滑动面板上下滑动

问滑动面板上下滑动
EN

Stack Overflow用户
提问于 2014-12-18 13:09:43
回答 3查看 926关注 0票数 1

我有下面的小提琴例子

代码语言:javascript
复制
<div class="panel" id="map">
  <a href="#" class="close">Close</a>
  <img src="http://placehold.it/880x120"/>    
</div>
<div class="panel" id="text">
  <a href="#" class="close">Close</a>
  Lorem ipsum dolor sit amet
</div>
<div class="content">
  <a href="#map" id="open-map" class="trigger">Open Map</a><br>
  <a href="#text" id="open-map" class="trigger">Open Text</a>
  <p>
    Integer mattis felis in felis eleifend ...       
  </p>
</div>

#map {border: solid 1px red;}

#text {border: solid 1px blue;}

img {display: block;}

div.panel {
  position: relative;
  overflow: hidden;
  display: none;    
}

div.panel a {
  background-color: yellow;
  color: @red;
  display: inline-block;
}

我需要以下几点:

  1. 单击触发器时,滑动/向上切换面板上的id,与其href中的相同。
  2. 当单击面板内的按钮关闭时,将该面板向上滑动。

我能够用以下方法解决这个问题:

代码语言:javascript
复制
$('#open-map').click(function(){
    var $helper = $('#map');
    $helper.animate({
        height: "toggle"
    }, {
        duration: 200,
        progress: function(){
           $helper.scrollTop( $helper[0].scrollHeight );       
        }
    });
});
  1. 我不知道如何使用触发器类和如何使关闭工作。
  2. 我也不确定动画是否是最好的选择。
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-18 13:46:42

另一种方法(只要将<a>元素的href属性与目标元素的id关联起来)使其更具可扩展性:

代码语言:javascript
复制
// binding a click-handler to the <a class="close"> elements:
$('a.close').on('click', function (e) {
    // slide-toggling the closest <div class="panel"> ancestor element,
    // although slideUp() could be used, since if the element is hidden the
    // a.close element can't be clicked:
    $(this).closest('.panel').slideToggle();
});

// binding a click handler to the .trigger elements:
$('.trigger').on('click', function () {
    // getting a reference to the clicked-element:
    var clicked = $(this),
        // forming an array of all the sibling .trigger elements,
        ids = clicked.siblings('.trigger')
                  // adding back the clicked element:
                  .addBack().map(function () {
                      return this.getAttribute('href');
                  }).get();
    // finding all the elements associated with those .trigger elements:
    $(ids.join(',')).each(function () {
        // iterating over the collection with each(),
        // if the href attribute (not property) of the clicked element
        // contains the string of the id of the current element at
        // an index of 1:
        if (clicked.attr('href').indexOf(this.id) === 1) {
            // we slideToggle() the relevant element:
            $(this).slideToggle();
        } else {
            // otherwise we simply close the element:
            $(this).slideUp();
        }
    });
});

​

代码语言:javascript
复制
$('a.close').on('click', function(e) {
  $(this).closest('.panel').slideToggle();
});

$('.trigger').on('click', function() {
  var clicked = $(this),
    ids = clicked.siblings('.trigger').addBack().map(function() {
      return this.getAttribute('href');
    }).get();
  $(ids.join(',')).each(function() {
    if (clicked.attr('href').indexOf(this.id) === 1) {
      $(this).slideToggle();
    } else {
      $(this).slideUp();
    }
  });
});
代码语言:javascript
复制
.panel {
  display: none;
}
.trigger {
  display: block;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel" id="map"> <a href="#" class="close">Close</a>
  <img src="http://placehold.it/880x120" />
</div>
<div class="panel" id="text"> <a href="#" class="close">Close</a>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce imperdiet, urna id dignissim venenatis, mi metus convallis purus, et tincidunt nunc leo sit amet lectus. Nullam vitae efficitur purus. Nulla facilisi. Praesent mauris massa, ullamcorper ut
  varius vitae, porttitor id augue.</div>
<div class="content"> <a href="#map" id="open-map" class="trigger">Open Map</a>
  <a href="#text" id="open-text" class="trigger">Open Text</a>

</div>

​

参考文献:

​

​

票数 1
EN

Stack Overflow用户

发布于 2014-12-18 13:17:23

工作小提琴

代码语言:javascript
复制
$('#open-map').click(function(){
    $('#map').slideToggle('slow');
});
$('#open-text').click(function(){
    $('#text').slideToggle('slow');
});
$('.close').click(function(){
    $(this).parent().slideUp('slow');
});

同时适用于地图和文本。

票数 2
EN

Stack Overflow用户

发布于 2014-12-18 13:16:30

代码语言:javascript
复制
$('#close-map').click(function(){
var $helper = $('#map');
$helper.animate({
            height: "toggle"
        }, {
        duration: 200,
        progress: function(){
            $helper.scrollTop( $helper[0].scrollHeight );       
        }
    });
});

只需添加此块以使关闭链接工作,其中# close -map是关闭链接的id。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27547370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档