Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 乱人伦在线资源中文字幕,国产成人精品午夜免费,亚洲激情中文字幕

          整合營(yíng)銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          JS 鼠標(biāo)框選(頁(yè)面選擇)時(shí)返回對(duì)應(yīng)的 HTML 或文本內(nèi)容

          S 鼠標(biāo)框選(頁(yè)面選擇)時(shí)返回對(duì)應(yīng)的 HTML 或文案內(nèi)容

          一、需求背景

          1、項(xiàng)目需求

          當(dāng)用戶進(jìn)行鼠標(biāo)框選選擇了頁(yè)面上的內(nèi)容時(shí),把選擇的內(nèi)容進(jìn)行上報(bào)。

          2、需求解析

          雖然這需求就一句話的事,但是很顯然,沒(méi)那么簡(jiǎn)單...

          因?yàn)槭髽?biāo)框選說(shuō)起來(lái)簡(jiǎn)單,就是選擇的內(nèi)容,但是這包含很多中情況,比如:只選擇文案、選擇圖片、選擇輸入框、輸入框中的內(nèi)容選擇、iframe、等。

          簡(jiǎn)單總結(jié),分為以下幾點(diǎn):

          1. 選擇文案時(shí)
          2. 選擇圖片、svg、iframe、video、audio 等標(biāo)簽時(shí)
          3. 選擇 input、select、textarea 等標(biāo)簽時(shí)
          4. 選擇 input、textarea 標(biāo)簽內(nèi)容時(shí)
          5. 選擇類似 字符時(shí)
          6. 鍵盤(pán)全選時(shí)
          7. 鼠標(biāo)右鍵選擇
          8. 以上各模塊結(jié)合時(shí)
          9. 當(dāng)包含標(biāo)簽的時(shí)候,返回 html 結(jié)構(gòu),只有文本時(shí)返回文本內(nèi)容

          二、技術(shù)要點(diǎn)

          鼠標(biāo)框選包含以下幾點(diǎn):

          1. debounce 防抖
          2. addEventListener 事件監(jiān)聽(tīng)
          3. Range 對(duì)象
          4. Selection 對(duì)象

          1、debounce

          老生常談的技術(shù)點(diǎn)了,這里不能用節(jié)流,因?yàn)榭隙ú荒苣闶髽?biāo)選擇的時(shí)候,隔一段時(shí)間返回一段內(nèi)容,肯定是選擇之后一起返回。

          這里用 debounce 主要也是用在事件監(jiān)聽(tīng)和事件處理上。

          • 【debounce 掘金】
          • 【debounce CSDN】

          2、addEventListener

          事件監(jiān)聽(tīng),因?yàn)槭髽?biāo)選擇,不僅僅是鼠標(biāo)按下到鼠標(biāo)抬起,還包括雙擊、右鍵、全選。

          需要使用事件監(jiān)聽(tīng)對(duì)事件作處理。

          • 【addEventListener MDN】

          3、Range

          Range 接口表示一個(gè)包含節(jié)點(diǎn)與文本節(jié)點(diǎn)的一部分的文檔片段。

          Range 是瀏覽器原生的對(duì)象。

          3.1. 創(chuàng)建 Range 實(shí)例,并設(shè)置起始位置

          <body>
            <ul>
              <li>Vite</li>
              <li>Vue</li>
              <li>React</li>
              <li>VitePress</li>
              <li>NaiveUI</li>
            </ul>
          </body>
          <script>
            // 創(chuàng)建 Range 對(duì)象
            const range = new Range()
            const liDoms = document.querySelectorAll("li");
            // Range 起始位置在 li 2
            range.setStartBefore(liDoms[1]);
            // Range 結(jié)束位置在 li 3
            range.setEndAfter(liDoms[2]);
            // 獲取 selection 對(duì)象
            const selection = window.getSelection();
            // 添加光標(biāo)選擇的范圍
            selection.addRange(range);
          </script>


          可以看到,選擇內(nèi)容為第二行和第三行

          3.1.1 瀏覽器兼容情況


          3.2. Range 屬性

          1. startContainer:起始節(jié)點(diǎn)。
          2. startOffset:起始節(jié)點(diǎn)偏移量。
          3. endContainer:結(jié)束節(jié)點(diǎn)。
          4. endOffset:結(jié)束節(jié)點(diǎn)偏移量。
          5. collapsed:范圍的開(kāi)始和結(jié)束是否為同一點(diǎn)。
          6. commonAncestorContainer:返回完整包含 startContainer 和 endContainer 的最深一級(jí)的節(jié)點(diǎn)。

          3.2.1. 用我們上面創(chuàng)建的實(shí)例來(lái)看下 range 屬性的值


          3.2.2. 如果我們只選擇文本內(nèi)容時(shí)

          只選擇 li 中的 itePres


          可以看出 range 屬性對(duì)應(yīng)的值


          3.3. Range 方法

          1. cloneContents():復(fù)制范圍內(nèi)容,并將復(fù)制的內(nèi)容作為 DocumentFragment 返回。
          2. cloneRange():創(chuàng)建一個(gè)具有相同起點(diǎn)/終點(diǎn)的新范圍, 非引用,可以隨意改變,不會(huì)影響另一方。
          3. collapse(toStart):如果 toStart=true 則設(shè)置 end=start,否則設(shè)置 start=end,從而折疊范圍。
          4. compareBoundaryPoints(how, sourceRange):兩個(gè)范圍邊界點(diǎn)進(jìn)行比較,返回一個(gè)數(shù)字 -1、0、1。
          5. comparePoint(referenceNode, offset):返回-1、0、1具體取決于 是 referenceNode 在 之前、相同還是之后。
          6. createContextualFragment(tagString):返回一個(gè) DocumentFragment。
          7. deleteContents():刪除框選的內(nèi)容。
          8. extractContents():從文檔中刪除范圍內(nèi)容,并將刪除的內(nèi)容作為 DocumentFragment 返回。
          9. getBoundingClientRect():和 dom 一樣,返回 DOMRect 對(duì)象。
          10. getClientRects():返回可迭代的對(duì)象序列 DOMRect。
          11. insertNode(node):在范圍的起始處將 node 插入文檔。
          12. intersectsNode(referenceNode):判斷與給定的 node 是否相交。
          13. selectNode(node):設(shè)置范圍以選擇整個(gè) node。
          14. selectNodeContents(node):設(shè)置范圍以選擇整個(gè) node 的內(nèi)容。
          15. setStart(startNode, startOffset):設(shè)置起點(diǎn)。
          16. setEnd(endNode, endOffset):設(shè)置終點(diǎn)。
          17. setStartBefore(node):將起點(diǎn)設(shè)置在 node 前面。
          18. setStartAfter(node):將起點(diǎn)設(shè)置在 node 后面。
          19. setEndBefore(node):將終點(diǎn)設(shè)置為 node 前面。
          20. setEndAfter(node):將終點(diǎn)設(shè)置為 node 后面。
          21. surroundContents(node):使用 node 將所選范圍內(nèi)容包裹起來(lái)。

          3.4. 創(chuàng)建 Range 的方法

          3.4.1. Document.createRange

          const range = document.createRange();

          3.4.2. Selection 的 getRangeAt() 方法

          const range = window.getSelection().getRangeAt(0)

          3.4.3. caretRangeFromPoint() 方法

          if (document.caretRangeFromPoint) {
              range = document.caretRangeFromPoint(e.clientX, e.clientY);
          }

          3.4.4. Range() 構(gòu)造函數(shù)

          const range = new Range()

          3.5. Range 兼容性


          4、Selection

          Selection 對(duì)象表示用戶選擇的文本范圍或插入符號(hào)的當(dāng)前位置。它代表頁(yè)面中的文本選區(qū),可能橫跨多個(gè)元素。

          4.1. 獲取文本對(duì)象

          window.getSelection()



          4.2. Selection 術(shù)語(yǔ)

          4.2.1. 錨點(diǎn) (anchor)

          錨指的是一個(gè)選區(qū)的起始點(diǎn)(不同于 HTML 中的錨點(diǎn)鏈接)。當(dāng)我們使用鼠標(biāo)框選一個(gè)區(qū)域的時(shí)候,錨點(diǎn)就是我們鼠標(biāo)按下瞬間的那個(gè)點(diǎn)。在用戶拖動(dòng)鼠標(biāo)時(shí),錨點(diǎn)是不會(huì)變的。

          4.2.2. 焦點(diǎn) (focus)

          選區(qū)的焦點(diǎn)是該選區(qū)的終點(diǎn),當(dāng)你用鼠標(biāo)框選一個(gè)選區(qū)的時(shí)候,焦點(diǎn)是你的鼠標(biāo)松開(kāi)瞬間所記錄的那個(gè)點(diǎn)。隨著用戶拖動(dòng)鼠標(biāo),焦點(diǎn)的位置會(huì)隨著改變。

          4.2.3. 范圍 (range)

          范圍指的是文檔中連續(xù)的一部分。一個(gè)范圍包括整個(gè)節(jié)點(diǎn),也可以包含節(jié)點(diǎn)的一部分,例如文本節(jié)點(diǎn)的一部分。用戶通常下只能選擇一個(gè)范圍,但是有的時(shí)候用戶也有可能選擇多個(gè)范圍。

          4.2.4. 可編輯元素 (editing host)

          一個(gè)用戶可編輯的元素(例如一個(gè)使用 contenteditable 的 HTML 元素,或是在啟用了 designMode 的 Document 的子元素)。

          4.3. Selection 的屬性

          首先要清楚,選擇的起點(diǎn)稱為錨點(diǎn)(anchor),終點(diǎn)稱為焦點(diǎn)(focus)。

          1. anchorNode:選擇的起始節(jié)點(diǎn)。
          2. anchorOffset:選擇開(kāi)始的 anchorNode 中的偏移量。
          3. focusNode:選擇的結(jié)束節(jié)點(diǎn)。
          4. focusOffset:選擇開(kāi)始處 focusNode 的偏移量。
          5. isCollapsed:如果未選擇任何內(nèi)容(空范圍)或不存在,則為 true。
          6. rangeCount:選擇中的范圍數(shù),之前說(shuō)過(guò),除 Firefox 外,其他瀏覽器最多為1。
          7. type:類型:None、Caret、Range

          4.4. Selection 方法

          1. addRange(range): 將一個(gè) Range 對(duì)象添加到當(dāng)前選區(qū)。
          2. collapse(node, offset): 將選區(qū)折疊到指定的節(jié)點(diǎn)和偏移位置。
          3. collapseToEnd(): 將選區(qū)折疊到當(dāng)前選區(qū)的末尾。
          4. collapseToStart(): 將選區(qū)折疊到當(dāng)前選區(qū)的起始位置。
          5. containsNode(node, partlyContained): 判斷選區(qū)是否包含指定的節(jié)點(diǎn),可以選擇是否部分包含。
          6. deleteFromDocument(): 從文檔中刪除選區(qū)內(nèi)容。
          7. empty(): 從選區(qū)中移除所有范圍(同 `removeAllRanges()``,已廢棄)。
          8. extend(node, offset): 將選區(qū)的焦點(diǎn)節(jié)點(diǎn)擴(kuò)展到指定的節(jié)點(diǎn)和偏移位置。
          9. getRangeAt(index): 返回選區(qū)中指定索引處的 Range 對(duì)象。
          10. removeAllRanges(): 移除所有選區(qū)中的范圍。
          11. removeRange(range): 從選區(qū)中移除指定的 Range 對(duì)象。
          12. selectAllChildren(node): 選中指定節(jié)點(diǎn)的所有子節(jié)點(diǎn)。
          13. setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset): 設(shè)置選區(qū)的起始和結(jié)束節(jié)點(diǎn)及偏移位置。
          14. setPosition(node, offset):collapse 的別名

          4.5. Selection 兼容性


          三、項(xiàng)目實(shí)現(xiàn)

          1、實(shí)現(xiàn)思路

          1. 先獲取選擇的內(nèi)容,開(kāi)發(fā) getSelectContent 函數(shù)
          2. 對(duì)獲取的內(nèi)容進(jìn)行判斷,是否存在 selection 實(shí)例,沒(méi)有直接返回 null
          3. 判斷 selection 實(shí)例的 isCollapsed 屬性 沒(méi)有選中,對(duì) selection 進(jìn)行 toString().trim() 操作,判斷內(nèi)容 有內(nèi)容,直接返回 text 類型 無(wú)內(nèi)容,返回 null 有選中,則判斷內(nèi)容
          4. 判斷選中的內(nèi)容有沒(méi)有節(jié)點(diǎn) 沒(méi)有節(jié)點(diǎn),則和沒(méi)有選中一樣處理,進(jìn)行 toString().trim() 操作,判斷內(nèi)容 有內(nèi)容,直接返回 text 類型 無(wú)內(nèi)容,返回 null 有節(jié)點(diǎn),進(jìn)行 toString().trim() 操作,判斷內(nèi)容 沒(méi)有內(nèi)容,判斷是否有特殊節(jié)點(diǎn) 有 'iframe', 'svg', 'img', 'audio', 'video' 節(jié)點(diǎn),返回 html 類型 有 'input', 'textarea', 'select',判斷 value 值,是否存在 存在:返回 html 類型 不存在:返回 null 沒(méi)有特殊節(jié)點(diǎn),返回 null 有內(nèi)容,返回 html 類型
          5. 對(duì)鼠標(biāo) mousedown、mouseup 事件和 selectionchange、contextmenu、dblclick 事件進(jìn)行監(jiān)聽(tīng),觸發(fā) getSelectContent 函數(shù)
          6. 在需要的地方進(jìn)行 debounce 防抖處理

          2、簡(jiǎn)易流程圖


          2、Debounce 方法實(shí)現(xiàn)

          2.1. JS

          function debounce (fn, time = 500) {
            let timeout = null; // 創(chuàng)建一個(gè)標(biāo)記用來(lái)存放定時(shí)器的返回值
            return function () {
              clearTimeout(timeout) // 每當(dāng)觸發(fā)時(shí),把前一個(gè) 定時(shí)器 clear 掉
              timeout = setTimeout(() => { // 創(chuàng)建一個(gè)新的 定時(shí)器,并賦值給 timeout
                fn.apply(this, arguments)
              }, time)
            }
          }

          2.2. TS

          /**
           * debounce 函數(shù)類型
           */
          type DebouncedFunction<F extends (...args: any[]) => any> = (...args: Parameters<F>) => void
          /**
           * debounce 防抖函數(shù)
           * @param {Function} func 函數(shù)
           * @param {number} wait 等待時(shí)間
           * @param {false} immediate 是否立即執(zhí)行
           * @returns {DebouncedFunction}
           */
          function debounce<F extends (...args: any[]) => any>(
            func: F,
            wait = 500,
            immediate = false
          ): DebouncedFunction<F> {
            let timeout: ReturnType<typeof setTimeout> | null
            return function (this: ThisParameterType<F>, ...args: Parameters<F>) {
              // eslint-disable-next-line @typescript-eslint/no-this-alias
              const context = this
              const later = function () {
                timeout = null
                if (!immediate) {
                  func.apply(context, args)
                }
              }
              const callNow = immediate && !timeout
              if (timeout) {
                clearTimeout(timeout)
              }
              timeout = setTimeout(later, wait)
              if (callNow) {
                func.apply(context, args)
              }
            }
          }

          3、獲取選擇的文本/html 元素

          3.1. 獲取文本/html 元素

          nterface IGetSelectContentProps {
            type: 'html' | 'text'
            content: string
          }
          /**
           * 獲取選擇的內(nèi)容
           * @returns {null | IGetSelectContentProps} 返回選擇的內(nèi)容
           */
          const getSelectContent = (): null | IGetSelectContentProps => {
            const selection = window.getSelection()
            if (selection) {
              // 1. 是焦點(diǎn)在 input 輸入框
              // 2. 沒(méi)有選中
              // 3. 選擇的是輸入框
              if (selection.isCollapsed) {
                return selection.toString().trim().length
                  ? {
                      type: 'text',
                      content: selection.toString().trim()
                    }
                  : null
              }
              // 獲取選擇范圍
              const range = selection.getRangeAt(0)
              // 獲取選擇內(nèi)容
              const rangeClone = range.cloneContents()
              // 判斷選擇內(nèi)容里面有沒(méi)有節(jié)點(diǎn)
              if (rangeClone.childElementCount > 0) {
                // 創(chuàng)建 div 標(biāo)簽
                const container = document.createElement('div')
                // div 標(biāo)簽 append 復(fù)制節(jié)點(diǎn)
                container.appendChild(rangeClone)
                // 如果復(fù)制的內(nèi)容長(zhǎng)度為 0
                if (!selection.toString().trim().length) {
                  // 判斷是否有選擇特殊節(jié)點(diǎn)
                  const isSpNode = hasSpNode(container)
                  return isSpNode
                    ? {
                        type: 'html',
                        content: container.innerHTML
                      }
                    : null
                }
                return {
                  type: 'html',
                  content: container.innerHTML
                }
              } else {
                return selection.toString().trim().length
                  ? {
                      type: 'text',
                      content: selection.toString().trim()
                    }
                  : null
              }
            } else {
              return null
            }
          }
          /**
           * 判斷是否包含特殊元素
           * @param {Element} parent 父元素
           * @returns {boolean} 是否包含特殊元素
           */
          const hasSpNode = (parent: Element): boolean => {
            const nodeNameList = ['iframe', 'svg', 'img', 'audio', 'video']
            const inpList = ['input', 'textarea', 'select']
            return Array.from(parent.children).some((node) => {
              if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
              if (
                inpList.includes(node.nodeName.toLocaleLowerCase()) &&
                (node as HTMLInputElement).value.trim().length
              )
                return true
              if (node.children) {
                return hasSpNode(node)
              }
              return false
            })
          }

          3.2. 只需要文本

          /**
           * 獲取框選的文案內(nèi)容
           * @returns {string} 返回框選的內(nèi)容
           */
          const getSelectTextContent = (): string => {
            const selection = window.getSelection()
            return selection?.toString().trim() || ''
          }

          4、添加事件監(jiān)聽(tīng)

          // 是否時(shí)鼠標(biāo)點(diǎn)擊動(dòng)作
          let selectionchangeMouseTrack: boolean = false
          const selectionChangeFun = debounce(() => {
            const selectContent = getSelectContent()
            console.log('selectContent', selectContent)
            // todo... 處理上報(bào)
            selectionchangeMouseTrack = false
          })
          // 添加 mousedown 監(jiān)聽(tīng)事件
          document.addEventListener('mousedown', () => {
            selectionchangeMouseTrack = true
          })
          // 添加 mouseup 監(jiān)聽(tīng)事件
          document.addEventListener(
            'mouseup',
            debounce(() => {
              selectionChangeFun()
            }, 100)
          )
          // 添加 selectionchange 監(jiān)聽(tīng)事件
          document.addEventListener(
            'selectionchange',
            debounce(() => {
              if (selectionchangeMouseTrack) return
              selectionChangeFun()
            })
          )
          // 添加 dblclick 監(jiān)聽(tīng)事件
          document.addEventListener('dblclick', () => {
            selectionChangeFun()
          })
          // 添加 contextmenu 監(jiān)聽(tīng)事件
          document.addEventListener(
            'contextmenu',
            debounce(() => {
              selectionChangeFun()
            })
          )

          也可以進(jìn)行封裝

          /**
           * addEventlistener function 類型
           */
          export interface IEventHandlerProps {
            [eventName: string]: EventListenerOrEventListenerObject
          }
          
          let selectionchangeMouseTrack: boolean = false
          const eventHandlers: IEventHandlerProps = {
            // 鼠標(biāo) down 事件
            mousedown: () => {
              selectionchangeMouseTrack = true
            },
            // 鼠標(biāo) up 事件
            mouseup: debounce(() => selectionChangeFun(), 100),
            // 選擇事件
            selectionchange:  debounce(() => {
              if (selectionchangeMouseTrack) return
              selectionChangeFun()
            }),
            // 雙擊事件
            dblclick: () => selectionChangeFun(),
            // 右鍵事件
            contextmenu: debounce(() => selectionChangeFun())
          }
          Object.keys(eventHandlers).forEach((event) => {
            document.addEventListener(event, eventHandlers[event])
          })

          5、返回內(nèi)容

          5.1. 純文本內(nèi)容


          5.2. html 格式


          6. 完整 JS 代碼

          function debounce (fn, time = 500) {
            let timeout = null; // 創(chuàng)建一個(gè)標(biāo)記用來(lái)存放定時(shí)器的返回值
            return function () {
              clearTimeout(timeout) // 每當(dāng)觸發(fā)時(shí),把前一個(gè) 定時(shí)器 clear 掉
              timeout = setTimeout(() => { // 創(chuàng)建一個(gè)新的 定時(shí)器,并賦值給 timeout
                fn.apply(this, arguments)
              }, time)
            }
          }
          
          let selectionchangeMouseTrack = false
          document.addEventListener('mousedown', (e) => {
            selectionchangeMouseTrack = true
            console.log('mousedown', e)
          })
          document.addEventListener('mouseup', debounce((e) => {
            console.log('mouseup', e)
            selectionChangeFun()
          }, 100))
          document.addEventListener('selectionchange', debounce((e) => {
            console.log('selectionchange', e)
            if (selectionchangeMouseTrack) return
            selectionChangeFun()
          }))
          document.addEventListener('dblclick', (e) => {
            console.log('dblclick', e)
            selectionChangeFun()
          })
          document.addEventListener('contextmenu',debounce(() => {
            selectionChangeFun()
          }))
          
          const selectionChangeFun = debounce(() => {
            const selectContent = getSelectContent()
            selectionchangeMouseTrack = false
            console.log('selectContent', selectContent)
          })
          
          const getSelectContent = () => {
            const selection = window.getSelection();
            if (selection) {
              // 1. 是焦點(diǎn)在 input 輸入框
              // 2. 沒(méi)有選中
              // 3. 選擇的是輸入框
              if (selection.isCollapsed) {
                return selection.toString().trim().length ? {
                  type: 'text',
                  content: selection.toString().trim()
                } : null
              }
              // 獲取選擇范圍
              const range = selection.getRangeAt(0);
              // 獲取選擇內(nèi)容
              const rangeClone = range.cloneContents()
              // 判斷選擇內(nèi)容里面有沒(méi)有節(jié)點(diǎn)
              if (rangeClone.childElementCount > 0) {
                const container = document.createElement('div');
                container.appendChild(rangeClone);
                if (!selection.toString().trim().length) {
                  const hasSpNode = getSpNode(container)
                  return hasSpNode ? {
                    type: 'html',
                    content: container.innerHTML
                  } : null
                }
                return {
                  type: 'html',
                  content: container.innerHTML
                }
              } else {
                return selection.toString().trim().length ? {
                  type: 'text',
                  content: selection.toString().trim()
                } : null
              }
            } else {
              return null
            }
          }
          
          const getSpNode = (parent) => {
            const nodeNameList = ['iframe', 'svg', 'img', 'audio', 'video']
            const inpList = ['input', 'textarea', 'select']
            return Array.from(parent.children).some((node) => {
              if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
              if (inpList.includes(node.nodeName.toLocaleLowerCase()) && node.value.trim().length) return true
              if (node.children) {
                return getSpNode(node)
              }
              return false
            })
          }

          四、總結(jié)

          1. 鼠標(biāo)框選上報(bào)能監(jiān)控用戶在頁(yè)面的行為,能為后續(xù)的數(shù)據(jù)分析等提供便利
          2. 基于 JS 中的 Selection 和 Range 實(shí)現(xiàn)的,使用原生 JS
          3. 涉及到的操作比較多,包含鍵盤(pán)、鼠標(biāo)右鍵、全選等
          4. 能對(duì)框選的內(nèi)容進(jìn)行分類,區(qū)別 html 和 text,更方便的看出用戶選擇了哪些內(nèi)容

          大家介紹如何通過(guò) JS/CSS 實(shí)現(xiàn)網(wǎng)頁(yè)返回頂部效果。

          CSS 按鈕樣式:

          #myBtn {

          display: none; /* 默認(rèn)隱藏 */

          position: fixed;

          bottom: 20px;

          right: 30px;

          z-index: 99;

          border: none;

          outline: none;

          background-color: red; /* 設(shè)置背景顏色,你可以設(shè)置自己想要的顏色或圖片 */

          color: white; /* 文本顏色 */

          cursor: pointer;

          padding: 15px;

          border-radius: 10px; /* 圓角 */

          }

          標(biāo)簽

          首先放出html

          <body>

          <contain class="test1">

          <a name="topAnchor"></a>

          <div id="top">我是頂部</div>

          <div></div>

          <div></div>

          <div></div>

          <div></div>

          <div></div>

          </contain>

          <footer>

          <button id="backTop1">第一種方式回到頂部</button>

          <button id="backTop2">第二種方式回到頂部</button>

          <button id="backTop3">第三種方式回到頂部</button>

          </footer>

          </body>

          然后具體操作步驟如下

          1. 將a標(biāo)簽放到指定元素的附近
          2. 然后通過(guò)點(diǎn)擊事件生成 a 標(biāo)簽
          3. 觸發(fā)a標(biāo)簽事件
          4. 刪除a標(biāo)簽

          const backTop1 = document.getElementById("backTop1")


          backTop1.addEventListener("click", function (e) {

          let a = document.createElement("a")

          a.href = "#topAnchor"

          e.target.appendChild(a)

          a.onclick = function (e) {

          e.stopPropagation()

          }

          a.click()

          e.target.removeChild(a)

          })

          效果如下圖所示

          效果很明顯,在事件觸發(fā)之后,頁(yè)面立馬跑到的頂部,在交互性沒(méi)啥要求的時(shí)候,這種做法確實(shí)沒(méi)啥問(wèn)題,不過(guò)要求高了之后就不行了,會(huì)顯得有些突兀。

          scrollTo()

          此 api 需要傳遞 DOM元素相對(duì)于window的 left 和 top 的距離,此例子僅展示簡(jiǎn)單demo,只考慮 top 坐標(biāo)

          當(dāng)然它還有一個(gè) behavior 參數(shù),將其設(shè)置為 smooth 后,將會(huì)出現(xiàn)滑動(dòng)效果 步驟如下:

          1. 計(jì)算目標(biāo)元素距離頂部的距離
          2. 通過(guò)事件觸發(fā)

          代碼如下:

          const backTop2 = document.getElementById("backTop2")

          const TOP = document.getElementById("top")

          const y = TOP.offsetTop

          const backTop3 = document.getElementById("backTop3")

          backTop3.addEventListener("click", function (e) {

          window.scrollTo({ top: y, left: 0, behavior: 'smooth' })

          })

          效果如下圖所示

          從效果上來(lái)看,相較于a標(biāo)簽,該api支持動(dòng)畫(huà),使得頁(yè)面更絲滑

          Element.scrollIntoView()

          該 api 相較于上一個(gè),節(jié)點(diǎn)信息更加的明確,操作方法也更加的簡(jiǎn)潔,更利于后續(xù)的維護(hù)

          代碼如下

          const backTop2 = document.getElementById("backTop2")

          const TOP = document.getElementById("top")

          backTop2.addEventListener("click", function (e) {

          TOP.scrollIntoView({ behavior: "smooth" })

          })

          效果如下圖所示

          從效果上來(lái)看,該api和scrollTo的作用是一致的,但是從代碼結(jié)構(gòu)上來(lái)說(shuō),scrollIntoView會(huì)更加的簡(jiǎn)潔

          以上三種方法是我目前比較常用的,如有不同之處,還望諸君不吝賜教!


          作者:pengpeng

          鏈接:https://juejin.cn/post/6906142651121139719

          來(lái)源:掘金

          著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。


          主站蜘蛛池模板: 国产微拍精品一区二区| 无码精品人妻一区| 少妇人妻精品一区二区| 成人区人妻精品一区二区不卡网站| 美女啪啪一区二区三区| 日韩精品无码人妻一区二区三区 | 成人精品一区二区三区中文字幕| 日本免费一区二区三区四区五六区 | 91精品一区二区三区久久久久| 日韩在线一区二区三区免费视频| 国精产品999一区二区三区有限| 91一区二区视频| 日韩AV无码久久一区二区| 国产精品熟女一区二区| 国产激情一区二区三区| 亚洲一区二区精品视频| 一区二区三区四区无限乱码| 国产伦精品一区二区三区在线观看| 色窝窝无码一区二区三区| 亚洲熟妇av一区二区三区漫画| 无码精品蜜桃一区二区三区WW| AV怡红院一区二区三区| 精品福利一区3d动漫| 一区二区三区无码被窝影院| 无码囯产精品一区二区免费 | 水蜜桃av无码一区二区| 日韩一区二区电影| 久久一区不卡中文字幕| 亚洲一区二区三区在线观看蜜桃| 人妻无码久久一区二区三区免费| 国产日韩AV免费无码一区二区| 国产无线乱码一区二三区 | 在线成人综合色一区| 久久蜜桃精品一区二区三区| 怡红院美国分院一区二区| 视频在线一区二区三区| 中文字幕在线观看一区二区三区| 久久精品一区二区三区中文字幕| 久久久国产一区二区三区 | 国产一区二区三区小向美奈子| 国产伦精品一区三区视频|