原生JS實現(xiàn)分享側(cè)邊欄
更新時間:2021年10月18日 11:15:24 作者:aiguangyuan
這篇文章主要為大家詳細介紹了原生JS實現(xiàn)分享側(cè)邊欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文分享一個用原生JS實現(xiàn)的分享側(cè)邊欄,實現(xiàn)效果如下:
以下是代碼實現(xiàn),方便大家復(fù)制粘貼。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>分享到效果</title> <style> #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; } </style> </head> <body> <div id="share"> <span>分享到</span> </div> <script> // 獲取元素 var share = document.getElementById("share"); // 將事件設(shè)置給share share.onmouseover = function () { animate(this, "left", 0); }; share.onmouseout = function () { animate(this, "left", -100); }; // animate運動函數(shù) function animate(tag, attr, target) { clearInterval(tag.timer); tag.timer = setInterval(function () { // 獲取某個屬性的當前狀態(tài) // 由于具有單位,需要取整 // parseInt("hehe") => NaN NaN || 0 // 為了應(yīng)對auto轉(zhuǎn)換為NaN的問題,我們使用短路操作,保證程序的健壯性 var leader = parseInt(getStyle(tag, attr)) || 0; // 緩動公式的一部分是更改step的值 var step = (target - leader) / 10; // 由offsetLeft在取值的時候會四舍五入,step如果比較小,會造成無法運動的問題 // 根據(jù)步數(shù)的正負,更改取整方式 step = step > 0 ? Math.ceil(step) : Math.floor(step); // 緩動公式 leader = leader + step; // 設(shè)置給某一個屬性 tag.style[attr] = leader + "px"; // 檢測是否走到了指定位置 if (leader == target) { clearInterval(tag.timer); } }, 17); } // 用于獲取某個標簽的某個樣式屬性值 // 帶單位 function getStyle(tag, attr) { // 檢測支持哪一個 // box.currentStyle,如果不存在值為undefined // getComputedStyle如果瀏覽器不支持。相當于變量未聲明,報錯 if (tag.currentStyle) { // ie支持 return tag.currentStyle[attr]; } else { // 標準方法 return getComputedStyle(tag, null)[attr]; } } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JavaScript中實現(xiàn)無縫滾動、分享到側(cè)邊欄實例代碼
- JS運動框架之分享側(cè)邊欄動畫實例
- 博客側(cè)邊欄模塊跟隨滾動條滑動固定效果的實現(xiàn)方法(js+jquery等)
- javascript實現(xiàn)動態(tài)側(cè)邊欄代碼
- JavaScript實現(xiàn)簡單的隱藏式側(cè)邊欄功能示例
- javascript 實現(xiàn)動態(tài)側(cè)邊欄實例詳解
- 利用js編寫響應(yīng)式側(cè)邊欄
- JS實現(xiàn)側(cè)邊欄鼠標經(jīng)過彈出框+緩沖效果
- 基于slideout.js實現(xiàn)移動端側(cè)邊欄滑動特效
- js+css實現(xiàn)全屏側(cè)邊欄
相關(guān)文章
Bootstrap學(xué)習筆記 輪播(Carousel)插件
Bootstrap 輪播(Carousel)插件是一種靈活的響應(yīng)式的向站點添加滑塊的方式。這篇文章主要介紹了Bootstrap學(xué)習筆記 輪播(Carousel)插件,需要的朋友可以參考下2017-03-03easyui window refresh 刷新兩次的解決方法(推薦)
下面小編就為大家?guī)硪黄猠asyui window refresh 刷新兩次的解決方法(推薦)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05CSS中position屬性之fixed實現(xiàn)div居中
這篇文章主要介紹了CSS中position屬性之fixed實現(xiàn)div居中的相關(guān)資料,需要的朋友可以參考下2015-12-12KnockoutJS 3.X API 第四章之表單value綁定
Knockout是一個以數(shù)據(jù)模型(data model)為基礎(chǔ)的能夠幫助你創(chuàng)建富文本,響應(yīng)顯示和編輯用戶界面的JavaScript類庫。這篇文章主要介紹了KnockoutJS 3.X API 第四章之表單value綁定的相關(guān)資料,需要的朋友可以參考下2016-10-10Vue elementUI實現(xiàn)免密登陸與號碼綁定功能
這篇文章主要介紹了Vue elementUI實現(xiàn)免密登陸與號碼綁定功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習吧2022-11-11