jquery - 单击显示div,向下滑动到很多

标签 jquery html css onclick slide

所以,这就是我目前所拥有的:jsFiddle

HTML

<div style="height:1000px;">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div id="p_img" style="height:300px;">
    <div target="p1" class="portfolio">1</div>
    <div target="p2" class="portfolio">2</div>
    <div target="p3" class="portfolio">3</div>
</div>
<div>
    <div id="p1" class="description"> <a class="close">x</a>
        AAA : Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the     industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
    <div id="p2" class="description"> <a class="close">x</a>
        BBB : Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
    <div id="p3" class="description"> <a class="close">x</a>
        CCC : Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

JavaScript (jQuery)

$(document).ready(function () {
    $(".portfolio").click(function () {
        $('.description').slideUp();
        $('#' + $(this).attr('target')).slideDown();
        $('html, body').animate({
            scrollTop: $('#' + $(this).attr('target')).offset().top
        }, 600);
    });
    $(".close").click(function () {
        $('.description').slideUp();
        $('html, body').animate({
            scrollTop: $('#p_img').offset().top
        }, 600);
    });
});

CSS

.portfolio {
    float:left;
    width:100px;
    height:100px;
    background:#ccc;
    margin:10px;
    cursor:pointer;
}
.description {
    float:left;
    width:100%;
    font-size:12px;
    display:none;
}
.close {
    float:left;
    padding:5px;
    background:red;
}

它工作得很好,但有一个问题。

  1. 单击第一个灰色框,其下方会显示详细信息。
  2. 向上滚动(不要点击“x”按钮)并点击第二个灰色框。

问题:

你会看到页面向下滚动过多,没有到灰框2详情的顶部。

它应该做什么:

当 1 打开并单击 2 时,页面应向下滚动到 2 的详细信息。

最佳答案

我只会滚动到所有目标的父 div,而不是目标本身。

像这样

http://jsfiddle.net/NHskg/1/

我把动画改成了这个

$('html, body').animate({scrollTop: $('#targetWrapper').offset().top }, 600);

并给父div一个id

<div id='targetWrapper'>

关于jquery - 单击显示div,向下滑动到很多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17112234/

相关文章:

jquery - 如何通过JQuery改变CSS中伪元素的颜色?

php - 使用 JQuery 读取 Session 值

html - 是否可以在 Bootstrap 中向上和向下更改下拉箭头

html - 如何使用垂直对齐: middle

html - 加载 css 文件失败

html - 为什么浏览器会调整 HTML 输入元素的大小?

html - 为什么样式应用两次?

javascript - 如何使用 jquery 模板化 json

javascript - 过滤搜索列表未检索到不可见段落

jquery - bootstrap-datepicker 禁用当前月份的下个月 View