JS implements transparent gradient of navigation bar
This time I will bring you JS to realize the transparent gradient of the navigation bar. What are the precautions for realizing the transparent gradient of the navigation bar with JS. The following is a practical case, let's take a look.
mui has a built-in H5 version of the transparent gradient navigation control. For tutorials, please refer to the mui official website; transparent gradient navigation is a workaround to solve the problem of scroll bars reaching the top. Compared with dual webviews, it has higher performance and better performance. animation effect;
This article will share with you the implementation of MUI navigation bar transparent gradient effect based on native JS. The specific content details are as follows:
First of all, declare: Since the value of backgroundColor uses RGBA, which is not supported by IE8 and below, this effect does not support browsers of IE8 and below
css code
body,p,h1{margin: 0;} .module-layer{ width: 100%; position: fixed; top: 0; left: 0; z-index: 100000; } .module-layer-content{ position: relative; min-width: 320px; max-width: 750px; width: 100%; margin: 0 auto; background-color: rgba(255, 0, 0, 0.9); } .module-layer-bg{ width: 100%; height: 100%; background: #000; opacity: .7; position: absolute; top: 0; left: 0; z-index: -1; } .layer-head-name{ height: 50px; line-height: 50px; text-align: center; padding: 0 50px; font-size: 20px; } .layer-return,.layer-share{ width: 50px; height: 50px; line-height: 50px; text-align: center; position: absolute; top:0; z-index: 1; } .layer-return{left: 0;} .layer-share{right: 0;} .fixed-layer{ height: 100%; display: none; z-index: 100001; } .relative-layer{height: 100%;} .layer-content{ padding:3%; position: relative; top: 20%; } .layer-close{ width: 2rem; height: 2rem; position: absolute; top:3%; right: 3%; } .pr { position:relative; } #shop-input::-webkit-input-placeholder { color:#fff; } #shop-input:-moz-placeholder { color:#fff; } #shop-input::-moz-placeholder { color:#fff; } #shop-input:-ms-input-placeholder { color:#fff; } #shop-input { border:none; outline:none; background:transparent; } .search-box { height:30px; border-radius:20px; top:10px; overflow:hidden; z-index:10; } .search-box:after { content:''; display:block; width:100%; height:30px; background:#fff; opacity:.5; position:absolute; top:0; left:0px; z-index:-1; } #shop-input { height:28px; line-height:28px; font-size:16px; position:absolute; top:0; left:30px; } .shop-search { width:16px; height:16px; position:absolute; top:7px; left:6px; } .layer-return{ background: url(images/return.png) no-repeat center center/12px 20px; } .layer-share{ background: url(images/share.png) no-repeat center center/20px 30px; } a { -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; -webkit-user-select: none; } .module-content{ min-width: 320px; max-width: 750px; width: 100%; margin: 0 auto; background: #fff; } .module-content p:first-child img{margin-top: 0;} .module-content p img{ display: block; width: 100%; margin-top: 10px; }
HTML code
<header> <p> </p> <p></p> <h1> </h1> <p> <img class="shop-search lazy" src="/static/imghwm/default1.png" data-src="images/search.png" alt="JS implements transparent gradient of navigation bar" > <input> </p> <p></p> </header> <p> </p><p><img src="/static/imghwm/default1.png" data-src="images/banner.png" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner1.png" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner3.png" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner4.jpg" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner5.png" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner6.png" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner7.jpg" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p> <p><img src="/static/imghwm/default1.png" data-src="images/banner8.jpg" class="lazy" alt="JS implements transparent gradient of navigation bar" ></p>
JS code
(function(){ //获取滚动条当前位置 function getScrollTop(){ var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0; if(document.body){ bodyScrollTop = document.body.scrollTop; } if(document.documentElement){ documentScrollTop = document.documentElement.scrollTop; } scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop; return scrollTop; } //获取CSS样式 function getStyle(element, attr){ if(element.currentStyle){ return element.currentStyle[attr]; }else{ return window.getComputedStyle(element,null)[attr]; } } //获取原始backgroundColor值 var color = getStyle(document.getElementsByClassName('module-layer-content')[0],'backgroundColor'); //取到RGB var colorRgb = color.substring(0,color.lastIndexOf(',') + 1); //取到A var colorA = color.substring(color.lastIndexOf(',') + 1,color.length - 1); //对A判断,如果最终值小于0.9,直接设置为1 if(colorA colorA) ? colorA : (getScrollTop() / 550)) + ')'; } //初始化函数 setCoverOpacity(); //绑定滚动监听事件 window.addEventListener('scroll',setCoverOpacity,false); }())
Notice:
Not compatible with IE8 and belowIE browser;
550 is the final transparency of the scroll bar's arrival position, which can be customized as needed;
The A of RGBA of CSS final background color is the final transparency
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
mint-ui loadmore Pull-up loading and pull-down refresh conflict handling method
Templates in ES6 Detailed explanation of string usage
The above is the detailed content of JS implements transparent gradient of navigation bar. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)