css 菜单栏悬停_在CSS中构建悬停菜单

css 菜单栏悬停

A good menu design is an important part of any website or web app UI. Using only modern HTML and CSS, all kinds of menu combinations can be created to handle whatever user interactions are necessary. In this article, we’ll take a look at how to build three different hover menus: a simple column menu, another one with nested sub-menus, and finally a cool radial “pie” menu with emoji icons.

良好的菜单设计是任何网站或Web应用程序UI的重要组成部分。 仅使用现代HTML和CSS,就可以创建各种菜单组合,以处理所需的任何用户交互。 在本文中,我们将研究如何构建三个不同的悬停菜单:一个简单的列菜单,另一个具有嵌套子菜单的菜单,以及一个带有表情符号图标的凉爽的径向“饼状”菜单。

For a copy of the example project used in this article, check out this repo.

有关本文使用的示例项目的副本,请查看此repo 。

概念 (Concepts)

Hover menus in CSS take advantage of the :hover pseudo-selector and various animations, transitions, and transforms to create the menu interactions. Complex menus can be created by properly sizing the menu components and adding interactions to them as necessary.

CSS中的悬停菜单利用:hover 伪选择器以及各种动画,过渡和转换来创建菜单交互。 可以通过适当调整菜单组件的大小并根据需要向其添加交互来创建复杂的菜单。

The basic idea is simple: when the mouse hovers over a menu, apply whatever effects are desired (such as expanding the menu or changing background color) and display the contents of the menu to the user. The contents of the menu can be arranged in various ways as we will examine shortly.

基本思想很简单:当鼠标悬停在菜单上时,应用所需的任何效果(例如,扩展菜单或更改背景颜色),然后向用户显示菜单的内容。 菜单的内容可以多种方式安排,我们将在稍后进行检查。

HTML (HTML)

First, let’s take a look at the project structure in index.html:

首先,让我们看一下index.html中的项目结构:

Aside from some basic document structure, we have definitions for three nav menus, each with a header element and varying content. The first menu is simple and contains four choices. The second menu is a little more complex and contains four sub-menus, each with four choices. The third contains emoji device icons that will be displayed around it in a circle.

除了一些基本的文档结构,我们还定义了三个nav菜单,每个菜单都有一个header元素和不同的内容。 第一个菜单很简单,包含四个选项。 第二个菜单稍微复杂一点,包含四个子菜单,每个子菜单有四个选择。 第三个包含表情符号设备图标,该图标将围成一圈显示。

The HTML for this example is kept simple by design and most of the magic happens in CSS, so let’s take a look at that next.

该示例HTML通过设计保持简单,大多数魔术发生在CSS中,因此接下来让我们看一下。

CSS (CSS)

The styles for the example project are located in style.css:

示例项目的样式位于style.css中

First up is the :root block, which contains CSS variables that will be used throughout the rest of the file. Variables are a powerful feature introduced in CSS Level 3 that greatly simplifies the task of organizing CSS code. Here we have a base size for navigation items as --nav-size that will set both the height and width of a closed navigation menu to 12vh(which is 12% of the viewport height). Background colors for the menus are also defined here.

首先是:root块,其中包含CSS变量 ,该变量将在文件的其余部分中使用。 变量是CSS Level 3中引入的一项强大功能,可大大简化组织CSS代码的任务。 此处,导航项的基本大小为--nav-size ,它将关闭的导航菜单的高度和宽度都设置为12vh (这是视口高度的12%)。 菜单的背景色也在此定义。

The item-fade-in animation will be used to show the hidden menu items by setting their display property to flex and fading-in the items with opacity.

item-fade-in动画将通过将隐藏菜单项的display属性设置为flex和淡入display不透明度来显示隐藏的菜单项。

Next, the body is styled, scaled to 100% height and width of the document, and set to display content in a horizontal flex container. The nav items (the root containers for each menu) are set to --nav-size and styled with various properties, and are also defined as flex containers to allow proper spacing of menu items. Note the transition on this element’s non-hover selector, which will cause the element to smoothly transition back to it’s default state (instead of just snapping back immediately). The nav > header selector sizes the menu header to --nav-size and flex is used once again, in this case to center the text inside the header both horizontally and vertically.

接下来,对body进行样式设置,缩放到文档的高度和宽度的100%,并设置为在水平flex容器中显示内容。 nav项(每个菜单的根容器)设置为--nav-size并具有各种属性样式,并且还定义为flex容器,以允许菜单项有适当的间距。 请注意,此元素的非悬停选择器上的transition ,这将导致该元素平稳过渡回其默认状态(而不是立即返回)。 nav > header选择器将菜单标nav > header大小调整为--nav-size并且再次使用flex,在这种情况下,可将文本在标头内水平和垂直居中。

Here’s where we start to get into the actual hover logic of the menu. The selector nav:hover causes the nav item, when hovered over, to transition it’s background color and height, using 120ms and 240ms linear transitions respectively. This demonstrates how multiple transitions can be chained inside a single CSS rule. Next, the first two menus are selected using the nth-child pseudo-class, to extend their transitions with height which will cause the menu to open vertically when activated. Menu items are controlled using the nav > div and related selectors, and these items are also set to center their own content using flex and respond to hover events by applying transitions and animations.

在这里,我们开始进入菜单的实际悬停逻辑。 选择器nav:hover使navnav:hover时,分别使用120ms240ms线性转换来转换其背景颜色和高度。 这说明了如何在单个CSS规则内链接多个转换。 接下来,使用第n个子伪类选择前两个菜单,以扩展其height转换,这将导致菜单在激活时垂直打开。 菜单项是使用nav > div和相关的选择器控制的,并且还可以使用flex将这些项设置为居中放置其内容,并通过应用过渡和动画来响应悬停事件。

The first menu is straightforward and simply drops upon hover, revealing a list of items that will each change background color when hovered over. The second menu is a little more complex and provides a list of horizontal expanding sub-menus when opened. This is achieved in the same manner as the basic menu effect, by selecting deeper into the menu and applying hover effects to show/hide sub-menu items using the same principles.

第一个菜单非常简单,只需将鼠标悬停在菜单上,就会显示一个菜单项,当鼠标悬停在菜单上时,它们都会改变背景颜色。 第二个菜单稍微复杂一点,打开后会提供水平扩展子菜单的列表。 这可以通过与菜单基本效果相同的方式来实现,方法是在菜单中选择更深的内容,然后使用相同的原理将鼠标悬停效果应用于显示/隐藏子菜单项。

The third menu takes a completely different approach, and does not change size when hovered but instead goes transparent, and applies effects to child elements that cause them to be rendered in a circle around the menu. This cool effect is achieved by sizing the menu items to the same size as the parent, using position: fixed to stack them vertically on top of each other, rotating each item by a factor of 90 degrees to encircle the menu, and applying transform: translateX(var(--nav-size)) to the child span element that will shift the span over by one “nav size unit”. The emoji icons are subsequently rotated as necessary, to re-orient them in the right direction (as the previous rotation places some of them sideways, upside-down, etc).

第三个菜单采用完全不同的方法,并且在悬停时不会更改大小,而是变为透明,并对子元素应用效果,使子元素在菜单周围以圆圈显示。 通过使用以下position: fixed将菜单项的大小调整为与父项相同的大小,可以达到以下效果position: fixed以将它们垂直堆叠在一起,将每个项旋转90度以包围菜单,然后应用transform: translateX(var(--nav-size))转换为子span元素,该元素将span移动一个“ nav size单位”。 随后根据需要旋转表情符号图标,以将其重新定向到正确的方向(因为之前的旋转将它们中的一些横向放置,上下颠倒等)。

This example illustrates just a few cool menus that can be designed and built purely in CSS. These menus can be modified or extended in any number of ways to produce interactions that feel right for the application at hand. Some of these concepts can be transferred to responsive mobile designs, replacing hover interactions with implementations of the CSS checkbox hack.

此示例仅说明了一些可以完全在CSS中设计和构建的很酷的菜单。 可以通过多种方式修改或扩展这些菜单,以产生适合手边应用程序的交互。 这些概念中的一些可以转移到响应式移动设计中,用CSS checkbox hack的实现代替悬停交互。

When it comes to HTML and CSS, the front-end developer is only limited by imagination (a healthy dose of patience also goes a long way). Thanks for reading and good luck with your next project!

在HTML和CSS方面,前端开发人员仅受想象力的限制(健康的耐心也有很长的路要走)。 感谢您的阅读并祝您下一个项目顺利!

翻译自: https://uxdesign.cc/building-hover-menus-in-css-fd901931f06f

css 菜单栏悬停

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274280.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一级学科和二级学科_在多学科团队中工作的6个障碍(以及如何解决这些问题)

一级学科和二级学科In a team with different skillsets, one can be hopeful and idealistic about the outcome. The goal is to work as one team, put users first and create awesome experiences. Unfortunately, things don’t always go as planned.在一支具有不同技能…

LBS核心技术解析(引子)

http://www.cnblogs.com/LBSer/archive/2013/04/25/3048754.html 引子: 人们常用“上知天文,下知地理”来形容一个人的博学,人们总是用三要素论“什么时间、什么地点,发生或干了什么事情”来描述一件事情,人们也常常借用“天时、地…

lynda ux_如何建立内部UX团队

lynda uxWritten by Cassandra Naji由卡珊德拉纳吉 ( Cassandra Naji)撰写 The needs of real users are increasingly driving enterprise software design and development. Since 2013, IBM has hired close to 1500 designers and UXers, establishing the largest design…

IE6下div宽高设置

IE6下宽高设置。IE下div 中没有内容时,设置宽高不起作用,必须设置div背景色,并使用滤镜。才能使Div填充目标区域。多用于,其他容器元素使用背景图片,但是背景图片的部分需要其他的事件支持。如跳转。可以使用放置div的…

财务软件开发_财务独立对软件开发人员的重要性

财务软件开发If you read this post, chances that you are a software developer who is seeking financial advice for smart money-saving or investment or early retirement.如果您阅读此文章,则您很可能是一名软件开发人员,正在为精明的省钱或投资…

WIP模块常用表结构

WIP模块常用表结构表名: wip.wip_accounting_classes说明: 离散作业会计科目CLASS_CODE VARCHAR2(10) 帐目ORGANIZATION_ID NUMBER 组织代码CLASS_TYPE NUMBER 帐目类型DESCRIPTION VARCHAR2(240) 描述…

book电子书数据库设计_如何为杀手book的封面设计写出完美的摘要

book电子书数据库设计逐步出版真正的假人 (BOOK PUBLISHING STEP BY STEP FOR REAL DUMMIES) I have spent 18 years in advertising, briefing designers and art directors on various projects — from the simplest typo-only banners to the most complex integrated camp…

5g的负面影响_设计系统的实施是否会对早期概念产生负面影响?

5g的负面影响Athe financial institution where I was recently working the design system was maintained in Sketch libraries and code. A small team working across multiple brands means there is always a question for why we may or may not maintain something. We…

每日英语:Five Really Dumb Money Moves You've Got to Avoid

You know the smartest things to do with your money. But what are the worst moves? What should you avoid?Weirdly enough, they are things that a surprising number of people are still doing─even though they probably know, in their heart of hearts, how fool…

像素/厘米与像素/英寸区别_像素/体素艺术入门指南

像素/厘米与像素/英寸区别Here’s some resources I’ve found helpful so you can start learning pixel or voxel art (as a continuation of this post on 3D resources). Feel free to mention anything I missed in the comments!这是我发现有帮助的一些资源,因…

畅通工程续 最短路

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。现在,已知起…

Vim中数字自增、自减

(1)ctrl a:数字自动增加1 按下ctrl a: (2)number ctrl a:数字自动增加number 例子:想将20修改成100,按下80 ctrl a: (3)ctrl x…

本土链雷达网_走向本土设计

本土链雷达网I’m old enough to remember when you could pick up any regional graphic design annual, open it, and know exactly which area you were looking at just from the work itself. Tennessee or Texas. Southern California or the Pacific Northwest. New Yor…

pyqt5子窗口跳出主窗口_弹出式窗口与 可用性,转换和跳出率

pyqt5子窗口跳出主窗口Written by Cassandra Naji由卡珊德拉纳吉 ( Cassandra Naji)撰写 They go by many names — modal windows, dialog boxes, modal pop-ups — but whatever you call them, pop-ups have a reputation for being divisive when it comes to usability. …

系统应用iPad设备应用需定制开发的3大理由

这几周朋友几篇文章介绍了改系统应用的文章. 关联文章的地址 iPad设备应用需定制发开的3大理由 通过深入分析iPad设备及iPad应用系统的实际情况,直接将现有的基于PC系统发开的信应用系统,直接迁移到iPad设备上,存在以下几方面的出突问题。也是…

移动硬盘不可用_如何对您的网站执行移动可用性审核

移动硬盘不可用In the wake of 在之后 Mobilegeddon, ensuring a good experience for your mobile users is more important than ever. With Google now giving preference to mobile-friendly sites in the search rankings, now is the time to ensure optimal mobile per…

开源服务器Tornado的初步了解

文章结束给大家来个程序员笑话:[M] 明天看了下Python的一个新web框架,由Facebook开源。不得不说,品牌效应啊,只要是Facebook开源的目项,没有不好用的。Tornado可以说是好用到了极致,从开打官方面页开始懂得…

开发人员安全行为规则_为开发人员改善生活的7条简单规则

开发人员安全行为规则As people, we have a superpower; it is the power of perception. As the famous saying goes, life is 10% what happens to us and 90% how we respond to it.作为人,我们拥有超级大国; 这是感知的力量。 俗话说,生活…

多线程的那群“象”

最初学习多线程的时候,只学了用Thread这个类,记忆中也用过Mutex,到后来只记得Thread的使用,其余的都忘了。知道前不久写那个Socket连接池时遇到了一些对象如:Semaphore,Interlocked,Mutex等&…

反馈页面设计_获得设计反馈的艺术

反馈页面设计I’m going to assume that you already know the importance of sharing work early and often to get feedback from your team and stakeholders. And how it’s such a critical part of the design process. Cool. Glad we’re on the same page.我假设您已经…