jQuery - SlideToggle,带有关闭按钮的页面滚动?

发布于 2024-12-25 07:50:58 字数 1788 浏览 4 评论 0原文

我是 jQuery 新手。我正在使用 .slideToggle 使 DIV 出现在所有内容的底部。我已经获得了使 DIV 出现的代码,以及向下滚动到该 DIV 的页面。

但是,我想制作一个额外的按钮/链接,然后隐藏内容并向上滚动。

我获得的代码允许我完成所有这些操作,但只有一个链接可以切换显示和隐藏,这不是很有用,因为我现在需要将隐藏链接置于所有内容的底部。

如果您对如何执行此操作有任何想法,请告诉我。

jQuery 代码

var height = $('#slickbox').hide().height();
$('.more a').toggle(function() {
    $('#slickbox').slideDown(3200);
    $('html, body').animate({ scrollTop: '+=' + height }, 3200);
    return false;
}, function() {
    $('#slickbox').slideUp(3200);
    $('html, body').animate({ scrollTop: '-=' + height }, 3200);
    return false;
});

HTML

<div style="height: 300px; border: solid 1px #ddd;">I'm using space!</div>
<div class="donate done">
    <h3 class="more"><a href="#" title="More"> More </a></h3>
</div>
<div id="slickbox">
    <div id="slide_show">
        <div id="slider">
            <ul>
                <li>
                    <img alt="figure 1" src="http://dummyimage.com/700x1300/000/fff&text=I'm+a+picture!,+woohoo!" width="700" height="1300" />
                </li>
                <li>
                    <img alt="figure 1" src="http://dummyimage.com/700x1300/000/fff&text=I'm+a+picture!,+woohoo!" width="700" height="1300" />
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="close">
    <h3 class="more"><a href="#" title="More">Close</a></h3>
</div>

以下是我到目前为止所得到的示例: http://jsfiddle.net/eMYJx/ 47/

当您单击“更多”按钮时,所有内容都会按照我想要的方式下降和滚动。但是当你按下关闭按钮时,你必须按两次。有什么办法可以解决这个问题吗?

I'm new to jQuery. I'm using .slideToggle to make a DIV appear at the bottom of all my content. I have got the code to make the DIV appear, and the page to scroll down to that DIV.

However, I want to make an additional button/link that would then hide the content and scroll back up.

The code I have got allows me to do all this but with only one link that toggles the show and hide, which isn't very useful as I need the hide link to be at the bottom of all my content now.

Please let me know if you have any ideas of how to do this.

jQuery Code

var height = $('#slickbox').hide().height();
$('.more a').toggle(function() {
    $('#slickbox').slideDown(3200);
    $('html, body').animate({ scrollTop: '+=' + height }, 3200);
    return false;
}, function() {
    $('#slickbox').slideUp(3200);
    $('html, body').animate({ scrollTop: '-=' + height }, 3200);
    return false;
});

HTML

<div style="height: 300px; border: solid 1px #ddd;">I'm using space!</div>
<div class="donate done">
    <h3 class="more"><a href="#" title="More"> More </a></h3>
</div>
<div id="slickbox">
    <div id="slide_show">
        <div id="slider">
            <ul>
                <li>
                    <img alt="figure 1" src="http://dummyimage.com/700x1300/000/fff&text=I'm+a+picture!,+woohoo!" width="700" height="1300" />
                </li>
                <li>
                    <img alt="figure 1" src="http://dummyimage.com/700x1300/000/fff&text=I'm+a+picture!,+woohoo!" width="700" height="1300" />
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="close">
    <h3 class="more"><a href="#" title="More">Close</a></h3>
</div>

Here is an example of what I've got so far: http://jsfiddle.net/eMYJx/47/

When you click the more button, everything drops and scrolls the way I want. But when you press the close button, you have to press it twice. Any ideas on a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2025-01-01 07:50:58

我会这样做。使用一个可在“更多”/“关闭”之间切换的链接。我使用动画回调来进行切换。

http://jsfiddle.net/eMYJx/49/

var height = $('#slickbox').hide().height();
$('.more a').toggle(function() {
    var $this = $(this);
    $('#slickbox').slideDown(3200);
    $('html, body').animate({ scrollTop: '+=' + height }, 3200, function() {
         $this.html('Close');
    });     
    return false;
}, function() {
    var $this = $(this);
    $('#slickbox').slideUp(3200);
    $('html, body').animate({ scrollTop: '-=' + height }, 3200, function(){
        $this.html('More');
    });
    $(this).html('More');
    return false;
});

或者,如果您想保留这两个按钮,您可以将单独的处理程序绑定到每个处理程序。两者都用于单击,但一个“更多”将执行切换代码的​​第一部分,“关闭”将执行后半部分。

I would do it like this. Use one link that you toggle between More/Close. I use the animation callback to make the switch.

http://jsfiddle.net/eMYJx/49/

var height = $('#slickbox').hide().height();
$('.more a').toggle(function() {
    var $this = $(this);
    $('#slickbox').slideDown(3200);
    $('html, body').animate({ scrollTop: '+=' + height }, 3200, function() {
         $this.html('Close');
    });     
    return false;
}, function() {
    var $this = $(this);
    $('#slickbox').slideUp(3200);
    $('html, body').animate({ scrollTop: '-=' + height }, 3200, function(){
        $this.html('More');
    });
    $(this).html('More');
    return false;
});

Alternatively if you want to keep both buttons you would bind separate handlers to each. Both for click, but one 'more' would execute the first part of your toggle code and 'close' would execute the second half.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文