简体   繁体   English

CSS/Javascript 自动滚动到顶部

[英]CSS/Javascript Auto Scroll To Top

I am currently working on a simple (or so I thought) slide-down menu for both the desktop and mobile versions of a website I am working on.我目前正在为我正在处理的网站的桌面版和移动版开发一个简单的(或者我认为是这样的)下拉菜单。 I'm trying to do this in CSS only if possible.仅在可能的情况下,我才尝试在 CSS 中执行此操作。 Currently the CSS I am using is as follows:目前我使用的CSS如下:

#menu {position: absolute; top: 0; left: 0; width: 100%; max-height: 50px; overflow: hidden; -webkit-transition: all 1s ease; /* For Safari 3.1 to 6.0 */ transition: all 1s ease; padding: 4px; }
#menu:hover {max-height: 75%; overflow: auto}

The reason I have the 'overflow: auto' when on a smaller screen is due to the entire menu not showing when on a smaller screen - even when I have some elements not showing on mobile screens.我在较小的屏幕上出现“溢出:自动”的原因是整个菜单在较小的屏幕上没有显示——即使我有一些元素没有在移动屏幕上显示。

The problem I'm running into is when it DOES overflow and the user scrolls through the menu, when they click/hover out of the menu it retracts like it should but it doesn't show the top of the menu anymore, it will show wherever they scrolled to.我遇到的问题是当它确实溢出并且用户滚动菜单时,当他们单击/悬停在菜单外时它会像应该的那样缩回但它不再显示菜单的顶部,它会显示无论他们滚动到哪里。

I'd like to find a way either via CSS or Javascript to automatically have that div scroll back to the top when the user clicks/hovers out of the menu so it will still show the menu icon + logo instead of random menu text.我想通过 CSS 或 Javascript 找到一种方法,当用户单击/悬停菜单时自动让该 div 滚动回顶部,因此它仍会显示菜单图标 + 徽标而不是随机菜单文本。

I love how it works but can't stand the scrolling issue on smaller screens.我喜欢它的工作方式,但无法忍受较小屏幕上的滚动问题。

There may be some awkwardness in your idea that might just need a redesign, I certainly have never noticed a menu working like this. 您的想法可能有些尴尬,可能只需要重新设计,我当然从来没有注意到菜单是这样工作的。

But the jQuery code to scroll to the top is this: 但是滚动到顶部的jQuery代码是这样的:

$("html, body").animate({
    scrollTop: 0
}, 600);

Where 600 is how long it takes in ms. 600是多长时间(毫秒)。 You'll have to wrap this in a click() function or something so it triggers when someone presses a menu option. 您必须将其包装在click()函数或其他内容中,以便在有人按下菜单选项时触发它。 For example: 例如:

$('#menu a').click(function () {
    $("html, body").animate({
        scrollTop: 0
    }, 600);
});

And of course all of this code should be wrapped in a $(document).ready() (basic jQuery setup) I know you didn't mention jQuery in your question, but this is a popular approach to solving problems like this. 当然,所有这些代码都应该包装在$(document).ready()(基本jQuery设置)中,我知道您没有在问题中提及jQuery,但这是解决此类问题的一种流行方法。

There is another way to achieve auto scroll on click using html only.还有另一种方法可以仅使用 html 实现点击时自动滚动。

You just need to add an id to some div (the header maybe) and on the link you pass the #id to the href like so:您只需要向某个 div 添加一个 id(可能是 header),然后在链接上将 #id 传递给 href,如下所示:

html html

<div id="Header">your header</div>

<a href="#Header">Go to the top</a>

css css

/* Keyword values */
scroll-behavior: auto;
scroll-behavior: smooth;

I'd suggest to try out the scroll-behavior: smooth;我建议尝试scroll-behavior: smooth; it makes the transitions, well... smooth:D它使过渡,嗯......顺利:D

Here you can find all the documentation you need about this solution https://developer.mozilla.org/de/docs/Web/CSS/scroll-behavior在这里您可以找到有关此解决方案所需的所有文档https://developer.mozilla.org/de/docs/Web/CSS/scroll-behavior

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM