单击后滚动到顶部 [英] Scroll to top after click

查看:81
本文介绍了单击后滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开菜单并向下滚动时,将其关闭然后再次重新打开,菜单将停留在该位置,但是我希望每次您通过锚点链接将其打开时都在顶部.

When i open my menu and scroll it downwards, close it and reopen it again, the menu stays at that point, but i want it to be opened at top everytime you open it also with an anchor link.

我添加了此内容,以在单击链接时关闭菜单:

I've added this to close the menu when clicking the link:

$("li > a").on("click", function() {
        $(".drawer-hamburger").trigger("click");
    });

一切正常,除了重新打开后不会回到顶部.

Everything works fine, except that its not going to top after re-opening.

我尝试过:

$('#div').scrollTop();

但这没有效果.到目前为止,唯一起作用的是:

But this had no effect. The only thing that worked so far was:

window.location.reload(true)

但是我认为每次重新加载页面都是过大的.

But I think it's overkill to reload the page every time.

有没有一种方法可以关闭菜单,然后在重新打开后通过单击将菜单返回到顶部?

Is there a way to close the menu and return the menu to top after re-opening, on click?

所有与此主题有关,

将blivesta动画菜单变成非画布(按正文)菜单

但是因为这是一个单独的问题,所以我认为开始一个新主题会更好.

but because it's a separate question i thought it would be better to start a new topic.

让我更具体地介绍这种情况. 单击汉堡图标,打开菜单.该列表是可滚动的,因此当我下降一点时,单击锚点链接,菜单关闭,我就在锚点上.到目前为止,一切都很好. 当我重新打开菜单时,它仍然位于菜单中我滚动到的位置.但是我想在重新打开后再次位于菜单顶部,而不刷新我的页面.

Let me be more specific about the situation. I open my menu by clicking the hamburger icon. The list is scrollable so when i go down a bit, click the anchor link, the menu closes and i'm at my anchor point. So far so good. When i reopen the menu, it's still at the point in my menu, where i was scrolling to. But i want to be on top of the menu again after re-opening, without refreshing my page.

在此处观看固定结果: http://codepen.io/Jorus/pen/zBLBpy

Edit #2: Watch the fixed result here : http://codepen.io/Jorus/pen/zBLBpy

推荐答案

您需要转换为0px:

$("li > a").on("click", function() {
    $(".drawer-menu").css("transform","translate(0, 0) translateZ(0)");
    $(".drawer-hamburger").trigger("click");
});

已编辑

这篇关于单击后滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆