国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) web前端 css教程 如何在 Vue.js 中頁(yè)面背景上的鼠標(biāo)光標(biāo)周圍添加徑向漸變

如何在 Vue.js 中頁(yè)面背景上的鼠標(biāo)光標(biāo)周圍添加徑向漸變

Dec 03, 2024 pm 03:19 PM

How to add a radial gradient around the mouse cursor on the background of a page in Vue.js

為了給網(wǎng)站增添趣味,我決定在每次鼠標(biāo)沿著屏幕視圖移動(dòng)時(shí),實(shí)現(xiàn)在光標(biāo)周圍閃爍的徑向漸變。以下實(shí)現(xiàn)適用于使用 TypeScript 構(gòu)建的 Vue.js 項(xiàng)目。

為了達(dá)到這個(gè)結(jié)果,為了簡(jiǎn)單起見(jiàn),我還想使用一個(gè)用于設(shè)備檢測(cè)的庫(kù),我選擇了 ua-parser-js,確切地說(shuō)是 2.0.0 版本。

第二個(gè)珊瑚步驟是創(chuàng)建漸變組件,它必須是所有視圖的主要容器,因?yàn)樗鼘⑹菨u變發(fā)光的區(qū)域。

// src/components/Gradient.vue

<script lang="ts">
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { isMobile } from '../utils/device'

export default {
  name: 'GradientView',
  setup() {
    const getViewCentrePosition = () => ({
      x: window.innerWidth / 2,
      y: window.innerHeight / 2
    })

    const cursorPositionRef = ref(getViewCentrePosition())

    const updateCursorPosition = (event: MouseEvent) => {
      if (!isMobile()) {
        cursorPositionRef.value = {
          x: event.clientX,
          y: event.clientY
        }
      }
    }

    onMounted(() => {
      if (!isMobile()) {
        window.addEventListener('mousemove', updateCursorPosition)
      }
    })

    onUnmounted(() => {
      if (!isMobile()) {
        window.removeEventListener('mousemove', updateCursorPosition)
      }
    })

    const gradientPosition = computed(() => {
      return `${cursorPositionRef.value.x}px ${cursorPositionRef.value.y}px`
    })

    return {
      gradientPosition
    }
  },
  data() {
    return {
      isMobileClass: isMobile()
    }
  }
}
</script>

<template>
  <div
    :class="{ 'gradient--desktop': !isMobileClass, gradient: true }"
    :style="{ '--gradient-position': gradientPosition }"
  >
    <slot />
  </div>
</template>

<style scoped lang="scss">
div {
  .gradient.gradient--desktop {
    background-image: radial-gradient(
      circle at var(--gradient-position, 50% 50%),
      var(--tertiary-color),
      var(--secondary-color) 20%
    );
    width: 100vw;
    height: 100vh;
    overflow: scroll;

    @media (prefers-color-scheme: dark) {
      background-image: radial-gradient(
        circle at var(--gradient-position, 50% 50%),
        var(--tertiary-color),
        var(--primary-color) 20%
      );
    }
  }
}
</style>

讓我們理解代碼。在腳本部分,我有一個(gè)函數(shù),它只返回初始位置,即屏幕視圖的中心。它可以以不同的方式處理,例如隱藏,或者在第一次鼠標(biāo)觸發(fā)后可以通過(guò)動(dòng)畫出現(xiàn)在左上角位置。這是一個(gè)實(shí)施選擇。為了簡(jiǎn)單起見(jiàn),我將其添加到視圖的中心。

const getViewCentrePosition = () => ({
  x: window.innerWidth / 2,
  y: window.innerHeight / 2
})

然后,我創(chuàng)建一個(gè)對(duì)對(duì)象的反應(yīng)性引用,該對(duì)象將用于跟蹤通過(guò)鼠標(biāo)事件發(fā)生的光標(biāo)鼠標(biāo)移動(dòng)。

const cursorPositionRef = ref(getViewCentrePosition())

然后我實(shí)現(xiàn)了負(fù)責(zé)在每次光標(biāo)移動(dòng)后更新響應(yīng)式引用對(duì)象的函數(shù)。

const updateCursorPosition = (event: MouseEvent) => {
  if (!isMobile()) {
    cursorPositionRef.value = {
      x: event.clientX,
      y: event.clientY
    }
  }
}

該函數(shù)需要與鼠標(biāo)事件關(guān)聯(lián)。

onMounted(() => {
  if (!isMobile()) {
    window.addEventListener('mousemove', updateCursorPosition)
  }
})

onUnmounted(() => {
  if (!isMobile()) {
    window.removeEventListener('mousemove', updateCursorPosition)
  }
})

最后,我計(jì)算漸變位置的反應(yīng)值,該值將提供給元素本身的 css。

const gradientPosition = computed(() => {
  return `${cursorPositionRef.value.x}px ${cursorPositionRef.value.y}px`
})

請(qǐng)注意,在上述大部分部分中,我都會(huì)檢查檢測(cè)到的設(shè)備是否在移動(dòng)設(shè)備上,以避免不必要的無(wú)用計(jì)算。

之后,我在模板中創(chuàng)建漸變的 html,它充當(dāng)內(nèi)容的完整頁(yè)面包裝器,并且僅在需要時(shí)應(yīng)用相對(duì)的 css。

<template>
  <div
    :class="{ 'gradient--desktop': !isMobileClass, gradient: true }"
    :style="{ '--gradient-position': gradientPosition }"
  >
    <slot />
  </div>
</template>

這是CSS。我在這里為實(shí)現(xiàn)淺色和深色主題的網(wǎng)站提供了一個(gè)解決方案,我使用兩種顏色進(jìn)行過(guò)渡,在外部部分我使用 --primary-color 和 --secondary-color 顏色,而內(nèi)部部分是將兩個(gè)主題設(shè)置為 --tertiary-color。但是,選擇和調(diào)整是你的。

<style scoped lang="scss">
.gradient.gradient--desktop {
  background-image: radial-gradient(
    circle at var(--gradient-position, 50% 50%),
    var(--tertiary-color),
    var(--secondary-color) 20%
  );
  width: 100vw;
  height: 100vh;
  overflow: scroll;

  @media (prefers-color-scheme: dark) {
    background-image: radial-gradient(
      circle at var(--gradient-position, 50% 50%),
      var(--tertiary-color),
      var(--primary-color) 20%
    );
  }
}
</style>

最后,如前所述,這是唯一正在使用的用于檢測(cè)設(shè)備的實(shí)用程序。

// src/utils/device.ts

import { UAParser } from 'ua-parser-js'

export const isMobile = (): boolean => {
  const uap = new UAParser()
  return uap.getDevice().type === 'mobile'
}

更好的想法?很想聽(tīng)到他們的聲音。

以上是如何在 Vue.js 中頁(yè)面背景上的鼠標(biāo)光標(biāo)周圍添加徑向漸變的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

什么是'渲染障礙CSS”? 什么是'渲染障礙CSS”? Jun 24, 2025 am 12:42 AM

CSS會(huì)阻塞頁(yè)面渲染是因?yàn)闉g覽器默認(rèn)將內(nèi)聯(lián)和外部CSS視為關(guān)鍵資源,尤其是使用引入的樣式表、頭部大量?jī)?nèi)聯(lián)CSS以及未優(yōu)化的媒體查詢樣式。1.提取關(guān)鍵CSS并內(nèi)嵌至HTML;2.延遲加載非關(guān)鍵CSS通過(guò)JavaScript;3.使用media屬性優(yōu)化加載如打印樣式;4.壓縮合并CSS減少請(qǐng)求。建議使用工具提取關(guān)鍵CSS,結(jié)合rel="preload"異步加載,合理使用media延遲加載,避免過(guò)度拆分與復(fù)雜腳本控制。

外部與內(nèi)部CSS:最好的方法是什么? 外部與內(nèi)部CSS:最好的方法是什么? Jun 20, 2025 am 12:45 AM

thebestapphachforcssdepprodsontheproject'sspefificneeds.forlargerprojects,externalcsSissBetterDuoSmaintoMaintainability andReusability; forsMallerProjectsorsingle-pageApplications,InternaltCsmightBemoresobleable.InternalCsmightBemorese.it.it'sclucialtobalancepopryseceneceenceprodrenceprodrenceNeed

我的CSS必須在較低的情況下嗎? 我的CSS必須在較低的情況下嗎? Jun 19, 2025 am 12:29 AM

否,CSSDOESNOTHAVETOBEINLOWERCASE.CHOMENDENS,使用flowercaseisrecommondendendending:1)一致性和可讀性,2)避免使用促進(jìn)性技術(shù),3)潛在的Performent FormanceBenefits,以及4)RightCollaboraboraboraboraboraboraboraboraboraboraboraboraboraboraboraboraborationWithInteams。

CSS案例靈敏度:了解重要的 CSS案例靈敏度:了解重要的 Jun 20, 2025 am 12:09 AM

cssismostlycaseminemintiment,buturlsandfontfamilynamesarecase敏感。1)屬性和valueslikeColor:紅色; prenotcase-sensive.2)urlsmustmustmatchtheserver'server'scase,例如

什么是AutoPrefixer,它如何工作? 什么是AutoPrefixer,它如何工作? Jul 02, 2025 am 01:15 AM

Autoprefixer是一個(gè)根據(jù)目標(biāo)瀏覽器范圍自動(dòng)為CSS屬性添加廠商前綴的工具。1.它解決了手動(dòng)維護(hù)前綴易出錯(cuò)的問(wèn)題;2.通過(guò)PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設(shè)置browserslist、在構(gòu)建流程中啟用;4.注意事項(xiàng)有不手動(dòng)加前綴、保持配置更新、非所有屬性都加前綴、建議配合預(yù)處理器使用。

什么是CSS計(jì)數(shù)器? 什么是CSS計(jì)數(shù)器? Jun 19, 2025 am 12:34 AM

csscounterscanautomationallymentermentermentections和lists.1)usecounter-ensettoInitializize,反插入式發(fā)芽,andcounter()orcounters()

CSS:何時(shí)重要(何時(shí)不)? CSS:何時(shí)重要(何時(shí)不)? Jun 19, 2025 am 12:27 AM

在CSS中,選擇器和屬性名不區(qū)分大小寫,而值、命名顏色、URL和自定義屬性則區(qū)分大小寫。1.選擇器和屬性名不區(qū)分大小寫,例如background-color和Background-Color相同。2.值中的十六進(jìn)制顏色不區(qū)分大小寫,但命名顏色區(qū)分大小寫,如red有效而Red無(wú)效。3.URL區(qū)分大小寫,可能導(dǎo)致文件加載問(wèn)題。4.自定義屬性(變量)區(qū)分大小寫,使用時(shí)需注意大小寫一致。

什么是圓錐級(jí)函數(shù)? 什么是圓錐級(jí)函數(shù)? Jul 01, 2025 am 01:16 AM

theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.

See all articles