Get the App
SLTechnology News&Howtos  ›  Development  › 

How to share the sidebar with native JS

Shulou Source: shulou.com Published: 2022-06-03 04:36:48 10月12日 Update

This article focuses on "how to share the sidebar with native JS". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to share the sidebar with native JS.

The results are as follows:

The following is the code implementation, convenient for everyone to copy and paste.

Share the effect # share {position: fixed; width: 100px; height: 200px; background-color: lightblue; left:-100px; top: 100px;} # share span {width: 20px; height: 60px; line-height: 20px Text-align: center; left: 100px; top: 70px; position: absolute; background-color: yellow;} share to / / get element var share = document.getElementById ("share") / / set the event to share share.onmouseover = function () {animate (this, "left", 0);}; share.onmouseout = function () {animate (this, "left",-100);}; / / animate motion function function animate (tag, attr, target) {clearInterval (tag.timer) Tag.timer = setInterval (function () {/ / get the current status of an attribute / / because it has units, it needs to be rounded / / parseInt ("hehe") = > NaN NaN | | 0 / / to deal with the problem of converting auto to NaN, we use short circuit operation. Ensure the robustness of the program var leader = parseInt (getStyle (tag, attr)) | | 0 / / part of the retardation formula is to change the value of step var step = (target-leader) / 10; / / the value will be rounded by offsetLeft. If step is relatively small, it will cause problems that cannot be moved / / change the rounding method step = step > 0 according to the positive or negative number of steps? Math.ceil (step): Math.floor (step); / / ease formula leader = leader + step; / / set to an attribute tag.style [attr] = leader + "px" / / check whether you have reached the specified position if (leader = = target) {clearInterval (tag.timer);}}, 17) } / / used to get a style attribute value of a tag / / function getStyle (tag, attr) {/ / detect which / / box.currentStyle is supported, if there is no value of undefined / / getComputedStyle if the browser does not support it. Equivalent to variable undeclared, error if (tag.currentStyle) {/ / ie supports return tag.currentStyle [attr];} else {/ / standard method return getComputedStyle (tag, null) [attr] }} at this point, I believe you have a deeper understanding of "how to share the sidebar with native JS". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Side attribute support formula content unit effect method problem learning detection movement robust practical deeper rounded event code location element Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information NVidia Redmi Apple