当前位置: 首页 > 编程日记 > 正文

css菜单缓慢滑动_如何使用HTML,CSS和JavaScript构建滑动菜单栏

css菜单缓慢滑动

by Supriya Shashivasan

由Supriya Shashivasan

如何使用HTML,CSS和JavaScript构建滑动菜单栏 (How to build a sliding menu bar using HTML, CSS and JavaScript)

A menu is what you look for when you land at a website. It has options and gives you access to everything the website has to offer you. You would definitely say it is an important part of a website, right?

菜单是您进入网站时想要的。 它具有选项,使您可以访问网站所提供的所有内容。 您肯定会说它是网站的重要组成部分,对吧?

My friend Girish patil and I started a biweekly newsletter for fronted developers this month. The first newsletter features sliding menu bars, and so here I am writing about how we built it.

我和朋友吉里什·帕蒂尔 ( Girish patil)本月开始了双月刊 ,专为前沿开发人员撰写 。 第一个时事通讯以滑动菜单栏为特色,因此在这里我要写关于我们如何构建它的信息。

Before we begin, get a container in place for your whole webpage and design the width and height according to your requirements. Now, inside the container, you have to place a sliding menu. In this article, we’ll explain how to create a left sliding menu.

在我们开始之前,请为整个网页准备好一个容器,并根据您的要求设计宽度和高度。 现在,您必须在容器内放置一个滑动菜单。 在本文中,我们将说明如何创建向左滑动菜单。

让我们开始吧 (Let’s get started)

The HTML code for the slider is given below. It is a basic bare version.

滑块HTML代码如下。 它是基本的裸版本。

<div class="slider-container">
<a href="#" class="slider-trigger">   Click here   </a>
<div class="slider-parent">    <h1>Slider</h1>    <a href="https:/twitter.com/giyaletter">Twitter&lt;/a> <br>    &lt;a href="https:/twitter.com/s_omeal">@Supriya</a> <br>    <a href="https:/twitter.com/g__patil">@Girish</a> <br>   </div>
</div>

An anchor tag is used to open the menu when clicked on. This is what triggers the menu to open, so you can see why it’s called slider-trigger. The menu component is what lies in the slider-parent class.

单击时,使用锚标记打开菜单。 这就是触发菜单打开的原因,因此您可以了解为什么将其称为“ slider-trigger” 。 菜单组件是滑块父类中的内容。

Now design the menu bar in CSS. Pay attention to the design details.

现在,在CSS中设计菜单栏。 注意设计细节。

.slider-container {  position: relative; }  .slider-container .slider-parent {    height: 70vh;    max-width: 250px;    width: 100%;    background: #6C7A89;    position: absolute;    left: -250px;    top: 50px;    visibility: hidden;    opacity: 0;    pointer-events: none;    transition: .2s all linear; }   .slider-container .slider-parent.active {      visibility: visible;      pointer-events: inherit;      transition: .2s all ease-in-out;      opacity: 1;      left: 0; }

Let’s now break down the above snippet and discuss how it works.

现在让我们分解上面的代码片段,并讨论其工作原理。

Maxwidth defines the maximum width up to which the div can occupy. In a smaller window, it can occupy less that 250px. The div occupies 250px when the window is stretched all the way out on the screen.

Maxwidth定义div可以占用的最大宽度。 在较小的窗口中,它可以占用的像素数少于250px。 当窗口一直延伸到屏幕上时,div占据250px。

At times, the user might look at the website on a much smaller screen, so we want our div to resize accordingly.

有时,用户可能会在小得多的屏幕上浏览网站,因此我们希望div相应地调整大小。

Moving on, let’s look at why left : -250px? This is done to get that smooth sliding action for the menu. Notice that the value for left is negative, which tells us that the menu starts 250px to the left of the starting position (which is 0). So it is presently not in the visible area.

继续前进,让我们看看为什么要离开-250px? 这样做是为了使菜单获得平滑的滑动动作。 请注意,left的值为负,这告诉我们菜单从起始位置(即0)的左侧开始250px。 因此,它目前不在可见区域。

We don’t want the sliding menu to be seen at all, which is why we add opacity and make its visibility hidden. Everybody likes animation, and it gives an interesting visual feel. This animation can be done using the transition component.

我们根本不希望看到滑动菜单,这就是为什么我们添加不透明度并将其可见性隐藏的原因 。 每个人都喜欢动画,它给人一种有趣的视觉感受。 可以使用过渡组件完成此动画。

呀! 基本的滑块已完成! (YAYYY! The basic slider is done!)

Now that the basic slider is done, let’s understand what happens when the slider bar is active — that is, when the anchor tag that opens the menu bar is clicked.

现在已经完成了基本的滑块,让我们了解一下当滑块处于活动状态时,即单击打开菜单栏的锚标记时会发生什么。

Focus on the active class in the CSS code given above. Notice that the values for opacity and visibility are changed. This change is made to make the slider (which was previously hidden) visible on the screen.

专注于上面给出CSS代码中的活动类。 请注意, 不透明度 可见性已更改。 进行此更改是为了使滑块(以前隐藏)在屏幕上可见。

Also, you might wonder: why is it now left : 0? Previously, the slider was out of the screen. Now that we want the menu to start at the left side of the screen, we change the value of left to 0.

另外,您可能想知道:为什么现在左为0? 以前,滑块不在屏幕上。 现在我们希望菜单从屏幕的左侧开始,我们将left的值更改为0。

OH! The animation! Add the transition component again so that when the slider is active, it eases in from the left smoothly.

哦! 动画! 再次添加过渡组件,以便在激活滑块时从左侧平滑地移入。

It’s done! You have designed the components, so what is the next step? JavaScript! It’ll put all this into action.

完成! 您已经设计了组件,那么下一步是什么? JavaScript! 它将所有这些付诸实践。

添加一些JavaScript (Adding some JavaScript)

var sliderTrigger = document.getElementsByClassName("slider-trigger")[0];var slider = document.getElementsByClassName('slider-parent')[0];
sliderTrigger.addEventListener( "click" , function(el){
if(slider.classList.contains("active")){  slider.classList.remove("active"); }else{  slider.classList.add("active"); }
});

Let’s look into how JavaScript wraps everything and gets the slider working. We want the slider to open when the anchor tag slider-trigger is clicked. So we get that element into a variable sliderTrigger. Later on we get the whole slider element into the variable slider. Now, we add an event listener that implements a function when the sliderTrigger element is clicked.

让我们研究一下JavaScript如何包装所有内容并使滑块正常工作。 我们希望单击锚标签滑块触发时打开滑块 。 因此,我们将该元素放入可变的slideTrigger中 稍后,我们将整个Slider元素放入变量lider中 。 现在,我们添加一个事件侦听器,该事件侦听器在sliderTrigger时实现一个功能 单击元素。

sliderTrigger.addEventListener( "click" , function(el) {} );

The function that is written controls the mechanics of opening and closing the sliding menu bar. Remember that we had an active and a normal slider-parent class.

编写的函数控制打开和关闭滑动菜单栏的机制。 请记住,我们有一个活动的和一个正常的父滑块 类。

The hack we implement here is to add the active class when the sliderTrigger element is clicked, and remove the active class when the same element is clicked again. To do that we use the code given below, to check if the variable contains the class active.

我们在这里实现的技巧是在单击slideTrigger元素时添加活动类,并在再次单击同一元素时删除活动类。 为此,我们使用下面给出的代码来检查变量是否包含活动类。

slider.classList.contains("active")

If the value is true, we remove the class active from the list. What happens then? The sliding menu bar closes. If the value is false, we add the class active to the classlist. Now what happens? Yes, the sliding menu bar is displayed. It is that simple.

如果该值为true,则从列表中删除活动的类。 那会发生什么呢? 滑动菜单栏关闭。 如果值为false,则将活动类添加到类列表中。 现在会发生什么? 是的,将显示滑动菜单栏。 就这么简单。

slider.classList.add("active")
slider.classList.remove("active")

Voilà完成了!! 看看谁在鼓掌;) (Voilà it’s done!! Look who is clapping ;))

The working of the same code is shown below in the CodePen.

下面的CodePen中显示了相同代码的工作方式。

While this is a basic example, I’m sending out examples of more complex and different types of sliding menu bars in my newsletter.

尽管这是一个基本示例,但我在时事通讯中发送了更复杂和不同类型的滑动菜单栏的示例。

Github repo of Giyaletter

Giyaletter的Github回购

Twitter handle: Supriya S and Girish Patil

Twitter句柄: Supriya S和Girish Patil

Thank you. Happy coding :)

谢谢。 快乐的编码:)

Check out products by us:

通过我们查看产品:

paybackhub.com and certhive.com

paybackhub.com和certhive.com

翻译自: https://www.freecodecamp.org/news/how-to-build-a-sliding-menu-bar-using-html-css-and-javascript-669f0c1c37a7/

css菜单缓慢滑动

相关文章:

素数环问题---深度搜索遍历

1264: 素数环 时间限制: 1 Sec 内存限制: 128 MB提交: 29 解决: 8[提交][状态][讨论版]题目描述 有一个长度为n的环形序列由1,2,3,...,n组成&#xff0c;环中相邻两个整数和均为素数。你需要找到所有满足条件的环。输入 输入n表示环的长度&#xff08;n<16&#xff09;输出…

android之Notification通知

我们在用手机的时候&#xff0c;如果来了短信&#xff0c;而我们没有点击查看的话&#xff0c;是不是在手机的最上边的状态栏里有一个短信的小图标提示啊&#xff1f;你是不是也想实现这种功能呢&#xff1f;今天的Notification就是解决这个问题的。 package cn.com.chenzheng_…

7 OC 中class 类的结构

目录 一 OC 中class 的结构 https://opensource.apple.com/tarballs/objc4/ 在最新的objc源码中 化繁就简来看的话 是以下结构 struct objc_class : objc_object {objc_class(const objc_class&) delete;objc_class(objc_class&&) delete;void operator(con…

apple id无法创建_我们如何使用Apple的学习框架来创建我们的第一个应用程序

apple id无法创建by Jonata Corra由JonataCorra 我们如何使用Apple的学习框架来创建我们的第一个应用程序 (How we used Apple’s learning framework to create our first app) After one month of work, my team and I finished the first version of Echo, our tracker iOS…

个人作业1:小学四则运算——基于控制台

a.需求分析&#xff1a; 自动生成小学四则运算题目的命令行 “软件”&#xff0c;满足以下需求&#xff1a; 除了整数以外&#xff0c;还要支持真分数的四则运算&#xff0c;真分数的运算&#xff0c;例如&#xff1a;1/6 1/8 7/24运算符为 , −, , 并且要求能处理用户…

getchar返回int类型

#include <stdio.h> /* copy input to output; 2nd version */main(){int c;c getchar();while(c ! EOF){putchar(c);c getchar();}} 直觉告诉我getchar返回值应该是char类型的&#xff0c;这个地方为什么不能用char类型来存储getchar()的返回值呢&#xff1f; 其实文中…

8 iOS中KVO 的本质

前言本质 Automatic key-value observing is implemented using a technique called isa-swizzling 这计划的意思就是 自动的键值观察的实现基于 isa-swizzling 原理 1.KVO是基于runtime机制实现的 2.当某个类的属性对象第一次被观察时&#xff0c;系统就会在运行期动态地创…

完成工作表-使用Google Spreadsheets作为数据后端

by Gilad Dayagi通过吉拉德达亚吉 完成工作表-使用Google Spreadsheets作为数据后端 (Get Sheet Done — using Google Spreadsheets as your data backend) If you want to rapidly prototype your next web apps, try using Google Spreadsheets as your data backend.如果您…

BIEE-CSS样式大全

字体属性&#xff1a;(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到&#xff0c;只要用数值就可以&#xff0c;单位&#xff1a;PX、PD 样式 {font-style: oblique;}(偏斜体) italic;(斜体) normal;(正常) 行高 {line-height: normal;}(正常) 单位&…

基于verilog的FPGA编程经验总结(XILINX ISE工具)

1.用ISE仿真的时候.所用变量一定要初始化. ISE默认初始量为"XXXXX", 而Quarters是默认为"00000"的, 其实实际上, 下到FPGA里后也是默认为0的,只是可以说ISE严谨得令人DT吧.比如说用一个累加器, result ABresult ,必须保证在某一刻A, B, result都为定值时,…

6 OC 中的isa 指针

目录 一 isa 指针 二 类对象中的superclass 一 isa 指针 isa 指针 &#xff0c;OC 中的对象都是有的 如下图所示&#xff0c;实例对象isa 指针指向 类对象&#xff0c;类对象的isa 指针指向 元类对象 二 类对象中的superclass superclass 有什么用呢&#xff1f; 比如说创…

btf-raft共识算法_了解Raft共识算法:学术文章摘要

btf-raft共识算法by Shubheksha通过Shubheksha 了解Raft共识算法&#xff1a;学术文章摘要 (Understanding the Raft consensus algorithm: an academic article summary) This post summarizes the Raft consensus algorithm presented in the paper In Search of An Underst…

iOS asset 中定义颜色,xib中便捷访问

在aseet 中定义一个颜色 这样就可以在xib 中访问颜色了&#xff0c;这样就不用重复的去输入

三种序列化方式性能比较

一下代码比较了二进制序列化、xml序列化、Protobuf序列化的运行时间&#xff0c;可是代码显得十分冗余&#xff0c;是否有大神可以指点一二&#xff0c;万分感谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; usi…

mac mini 装UBUNTU后没有WIFI解决办法

1、在终端中运行如下命令&#xff0c;重新安装b43相关的全部驱动和firmware: 复制代码代码如下:sudo apt-get install bcmwl-kernel-source #Broadcom 802.11 Linux STA 无线驱动源sudo apt-get install broadcom-sta-commonsudo apt-get install broadcom-sta-sourcesudo apt-…

区块链c端应用小程序_区块链如何真正起作用? 我建立了一个应用程序向您展示。...

区块链c端应用小程序by Sean Han通过肖恩韩 区块链如何真正起作用&#xff1f; 我建立了一个应用程序向您展示。 (How does blockchain really work? I built an app to show you.) According to Wikipedia, a blockchain is:根据维基百科&#xff0c;一个区块链是&#xff1…

HDU 4913 Least common multiple

/* hdu4913 Least common multiple http://acm.hdu.edu.cn/showproblem.php?pid4913 离散化 线段树 统计逆序数思想 tips: 1、线段树中一定要到处都取模&#xff0c;否则wa。。。 2、lazy是乘积的形式出现&#xff0c;不是加和*/ #include <cstdio> #include <algori…

JS ES6 实用笔记

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 这篇博文我会一直更新。 一.导出导入的两中方式 1.export //demo1.js export const a 6 导入语法为&#xff1a; import {a} form demo1 2.export default //demo2.js export default const b 6 …

extjs editgrid增加一行

Ext.onReady(function(){ /* * EditorGridPanel的工作过程 * 1、用户点击单元格 * 2、单元格按照预设的组件显示单元格的内容并处于编辑状态 * 3、离开单元格的编辑状态 * 4、更新编辑后的内容&#xff0c;出现三角号表示已经被修改过 * 5、程序内部变化&#xff1a;将记录设置…

unity 骨骼击碎_保证击碎$ 100挑战的创新策略

unity 骨骼击碎by Glenn Gonda由Glenn Gonda 保证击碎$ 100挑战的创新策略 (A Creative Strategy Guaranteed to Crush the $100 Challenge) Before I became a software engineer, I made my living as a recording studio engineer. I have a non-traditional background an…

mac下安装libpng环境

用go写一个爬虫工具时需要使用一个go的库&#xff0c;而这个库有需要使用libpng库&#xff0c;不然编译就会提示说 png.h找不到等之类的信息&#xff0c;于是想到应该和windows一样需要安装gcc环境&#xff0c;然后让gcc里安装libpng这个库&#xff0c; 解决办法&#xff1a; 终…

linux oracle修改编码utf8

$ sqlplus /nolog SQL> connect sys/oracle as sysdba SQL> startup 如何设置ORACLE数据库的编码&#xff08;ZHS16GBK&#xff09;修改成UTF8 SQL> shutdown immediate; SQL> startup mount; SQL> alter system enable restricted session; SQL> alter sy…

Vue.js 数据绑定渲染Demo

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 <div id"app">{{ message }} </div>var app new Vue({el: #app,data: {message: Hello Vue!} })Hello Vue!

angular搭建项目步骤_建立健康的Angular项目应采取的步骤

angular搭建项目步骤by Ashish Gaikwad通过Ashish Gaikwad 建立健康的Angular项目应采取的步骤 (Steps you should take to build a healthy Angular project) 使用Jenkins SonarQube创建您的“ Angular Fitbit” (Create your “Angular Fitbit” with Jenkins SonarQube) …

数据库的三大范式和事物

来源&#xff1a;http://blog.csdn.net/w__yi/article/details/19934319 1.1 第一范式&#xff08;1NF&#xff09;无重复的列 1.2 第二范式&#xff08;2NF&#xff09;属性完全依赖于主键 [ 消除部分子函数依赖 ] 1.3 第三范式&#xff08;3NF&#xff09;属性不依赖于其它非…

过滤器和包装器

作者&#xff1a;禅楼望月 过滤器要做的事情 请求过滤器 完成安全检查 重新格式化请求首部或体 建立请求审计或日志响应过滤器 压缩响应流 追加或修改响应流 创建一个完全不同的响应注意不能把过滤器的顺序依赖性硬编码进程序中&#xff0c;它应该由DD控制。 过滤器很像Servlet…

Missing space before value for key 'path'vue.js解决空格报错

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 找到 webpack.base.config.js文件注释掉下面的东西&#xff01;&#xff01; module: { rules: [ /*{ test: /\.(js|vue)$/, loader: eslint-loader, enforce: "p…

现代hy-9600音响_从音响工程师到软件工程师-为什么我要学习编码

现代hy-9600音响by Kalalau Cantrell通过Kalalau Cantrell 从音响工程师到软件工程师-为什么我要学习编码 (From Sound Engineer to Software Engineer — Why I’m Learning to Code) I seriously started teaching myself to code several months ago. I say “seriously” …

微信服务号、公众号、企业号注册

转载于:https://www.cnblogs.com/zhoulaoshi/p/6536850.html

a标签onclick事件解析

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 简单介绍<a>标签的常用点击事件的写法及作用 a href"javascript:void(0);" οnclick"js_method()" //javascript:void(0);作用是返回undefined&#xff0c;地址不发生跳转&am…