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
1.1 介紹
福哥最近處理一個(gè)客戶的網(wǎng)站JS錯(cuò)誤發(fā)現(xiàn)了一個(gè)詭異的情況,就是前面載入了一個(gè)JQ的插件,后面調(diào)用這個(gè)插件,提示插件不是一個(gè)函數(shù)。
經(jīng)過(guò)一頓查詢,發(fā)現(xiàn)載入插件的地方有個(gè)“defer”屬性,查資料得知這個(gè)屬性告知瀏覽器在全部網(wǎng)頁(yè)都加載完成之后再加載這個(gè)插件代碼。
我去,全部加載完成之后再加載插件代碼,那么這里調(diào)用插件肯定失敗啊~~
正常模式下,先加載jquery庫(kù),后打印版本信息,一切順利~~
2.1 代碼
<script type="text/javascript" src="http://sample.com/js/jquery.js"></script>
<script>
console.log(jQuery.fn.jquery);
</script>
2.2 效果
3.1 代碼
現(xiàn)在給加載jquery庫(kù)使用延遲模式,再來(lái)打印版本信息,報(bào)錯(cuò)了。
因?yàn)榇蛴“姹拘畔⒌臅r(shí)候jquery庫(kù)還沒(méi)有加載,當(dāng)然是找不到了。
<script type="text/javascript" src="http://sample.com/js/jquery.js" defer></script>
<script>
console.log(jQuery.fn.jquery);
</script>
3.2 效果
要解決這個(gè)問(wèn)題,需要將代碼放入頁(yè)面加載完成后的位置執(zhí)行,這里使用的是onreadystatechange事件判斷的頁(yè)面加載狀態(tài)(因?yàn)閖Query被延遲了,只能用原生JS了)。
4.1 代碼
<script type="text/javascript" src="http://sample.com/js/jquery.js" defer></script>
<script>
document.onreadystatechange = function () {
if(this.readyState == "complete"){
console.log(jQuery.fn.jquery);
}
};
</script>
福哥今天給大家講解了關(guān)于HTML的延遲加載屬性defer的相關(guān)知識(shí),使用延遲加載可以“提高頁(yè)面加載速度”,讓用戶直觀上感覺(jué)頁(yè)面速度比較快!但是,如果使用不當(dāng)就會(huì)造成JS代碼錯(cuò)誤的問(wèn)題。
https://m.tongfu.net/home/35/blog/512979.html
幾個(gè)簡(jiǎn)單的加載中動(dòng)畫(huà)吧。
像前面三種都是相當(dāng)于幾個(gè)不同的點(diǎn)輪流來(lái)播放同一動(dòng)畫(huà):變大變小。css3里面有一個(gè)用于尺度變換的方法:scale(x,y):定義 2D 縮放轉(zhuǎn)換,改變?cè)氐膶挾群透叨?/strong>。
第四種就是一個(gè)小球從上往下跌落,再?gòu)椈厝?,在上面的時(shí)候速度最小,下面的時(shí)候速度最大。由于該小球只進(jìn)行了上下的移動(dòng),所以我們可以運(yùn)用:translateY(n):定義 2D 轉(zhuǎn)換,沿著 Y 軸移動(dòng)元素,從而實(shí)現(xiàn)小球沿Y方向來(lái)回移動(dòng)。
廢話不多說(shuō)了,上代碼。
首先,第一個(gè)加載中的動(dòng)畫(huà):
html Code
<div id="loading1">
<div class="demo1"></div>
<div class="demo1"></div>
<div class="demo1"></div>
<div class="demo1"></div>
<div class="demo1"></div>
</div>
css Code
.demo1 {
width: 4px;
height: 4px;
border-radius: 2px;
background: #68b2ce;
float: left;
margin: 0 3px;
animation: demo1 linear 1s infinite;
-webkit-animation: demo1 linear 1s infinite;
}
.demo1:nth-child(1){
animation-delay:0s;
}
.demo1:nth-child(2){
animation-delay:0.15s;
}
.demo1:nth-child(3){
animation-delay:0.3s;
}
.demo1:nth-child(4){
animation-delay:0.45s;
}
.demo1:nth-child(5){
animation-delay:0.6s;
}
@keyframes demo1
{
0%,60%,100% {transform: scale(1);}
30% {transform: scale(2.5);}
}
@-webkit-keyframes demo1
{
0%,60%,100% {transform: scale(1);}
30% {transform: scale(2.5);}
}
css Code
第二個(gè)動(dòng)畫(huà)和第一個(gè)動(dòng)畫(huà)大同小異,第一個(gè)動(dòng)畫(huà)是將小球整體變大變小,第二動(dòng)畫(huà)則是將小方塊的高度變大變小,而寬度不變:
html Code
<div id="loading2">
<div class="demo2"></div>
<div class="demo2"></div>
<div class="demo2"></div>
<div class="demo2"></div>
<div class="demo2"></div>
</div>
css Code
.demo2 {
width: 4px;
height: 6px;
background: #68b2ce;
float: left;
margin: 0 3px;
animation: demo2 linear 1s infinite;
-webkit-animation: demo2 linear 1s infinite;
}
.demo2:nth-child(1){
animation-delay:0s;
}
.demo2:nth-child(2){
animation-delay:0.15s;
}
.demo2:nth-child(3){
animation-delay:0.3s;
}
.demo2:nth-child(4){
animation-delay:0.45s;
}
.demo2:nth-child(5){
animation-delay:0.6s;
}
@keyframes demo2
{
0%,60%,100% {transform: scale(1);}
30% {transform: scaleY(3);}
}
@-webkit-keyframes demo2
{
0%,60%,100% {transform: scale(1);}
30% {transform: scaleY(3);}
}
css Code
第三個(gè)動(dòng)畫(huà)就需要將小球的位置定位一下,讓幾個(gè)小球整體上看起來(lái)圍成一個(gè)圓,然后就像第一個(gè)一樣使小球變大變?。?/p>
html Code
<div id="loading1">
<div class="demo1"></div>
<div class="demo1"></div>
<div class="demo1"></div>
<div class="demo1"></div>
<div class="demo1"></div>
</div>
css Code
#loading3 {
position: relative;
width: 50px;
height: 50px;
}
.demo3 {
width: 4px;
height: 4px;
border-radius: 2px;
background: #68b2ce;
position: absolute;
animation: demo3 linear 0.8s infinite;
-webkit-animation: demo3 linear 0.8s infinite;
}
.demo3:nth-child(1){
left: 24px;
top: 2px;
animation-delay:0s;
}
.demo3:nth-child(2){
left: 40px;
top: 8px;
animation-delay:0.1s;
}
.demo3:nth-child(3){
left: 47px;
top: 24px;
animation-delay:0.1s;
}
.demo3:nth-child(4){
left: 40px;
top: 40px;
animation-delay:0.2s;
}
.demo3:nth-child(5){
left: 24px;
top: 47px;
animation-delay:0.4s;
}
.demo3:nth-child(6){
left: 8px;
top: 40px;
animation-delay:0.5s;
}
.demo3:nth-child(7){
left: 2px;
top: 24px;
animation-delay:0.6s;
}
.demo3:nth-child(8){
left: 8px;
top: 8px;
animation-delay:0.7s;
}
@keyframes demo3
{
0%,40%,100% {transform: scale(1);}
20% {transform: scale(3);}
}
@-webkit-keyframes demo3
{
0%,40%,100% {transform: scale(1);}
20% {transform: scale(3);}
}
接下來(lái)是第四個(gè)動(dòng)畫(huà):
1 <div id="loading5">
2 <div class="demo5"></div>
3 </div>
#loading5 {
width: 20px;
height: 100px;
border-bottom: 1px solid #68b2ce;
}
.demo5 {
width: 20px;
height: 20px;
border-radius: 10px;
background: #68b2ce;
animation: demo5 cubic-bezier(0.5,0.01,0.9,1) 0.6s infinite alternate;
-webkit-animation: demo5 cubic-bezier(0.5,0.01,0.9,1) 0.6s infinite alternate;
}
@keyframes demo5
{
0%{
transform:translateY(0px);
-webkit-transform:translateY(0px);
}
100% {
transform:translateY(80px);
-webkit-transform:translateY(80px);
}
}
@-webkit-keyframes demo5
{
0%{
transform:translateY(0px);
-webkit-transform:translateY(0px);
}
100% {
transform:translateY(80px);
-webkit-transform:translateY(80px);
}
}
css Code
以上就是這幾個(gè)簡(jiǎn)單的加載中小動(dòng)畫(huà)的內(nèi)容了。
轉(zhuǎn)載 https://www.cnblogs.com/tangchan/p/7604594.html
一篇文章Stimulus 狀態(tài)管理,幻燈片顯示講述了Stimulus的狀態(tài)管理,接下來(lái)我們看看如何跟蹤外部資源的狀態(tài)。
有時(shí)候我們的controllers需要跟蹤外部的資源的狀態(tài),這里的外部指的是不在DOM或不在Stimulus中的任何東西。例如,我們可能需要發(fā)出HTTP請(qǐng)求,并在請(qǐng)求狀態(tài)變化時(shí)進(jìn)行響應(yīng)?;蛘呶覀兿M麊?dòng)一個(gè)定時(shí)器,然后當(dāng)controller斷開(kāi)連接時(shí)停止定時(shí)器。在本文,我們將解決這些問(wèn)題。
接下來(lái),我們學(xué)習(xí)一下,如何通過(guò)加載和插入遠(yuǎn)程HTML片段,來(lái)異步填充頁(yè)面的各個(gè)部分。
我們要?jiǎng)?chuàng)建一個(gè)通用的用于加載內(nèi)容的controller,所有的HTML內(nèi)容都是從服務(wù)器獲取的。然后我們將用它來(lái)加載一系列未讀的消息,就像你在郵箱里看到的那樣。
打開(kāi)public/index.html:
<div data-controller="content-loader"
data-content-loader-url-value="/messages.html"></div>
然后創(chuàng)建一個(gè)public/messages.html
<ol>
<li>New Message: Stimulus Launch Party</li>
<li>Overdue: Finish Stimulus 1.0</li>
</ol>
在真實(shí)應(yīng)用中,這個(gè)內(nèi)容是服務(wù)器動(dòng)態(tài)生成的,但是這里出于示范的目的,我們就用一個(gè)靜態(tài)文件。
現(xiàn)在我們實(shí)現(xiàn)一下我們的controller:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = { url: String }
connect() {
this.load()
}
load() {
fetch(this.urlValue)
.then(response => response.text())
.then(html => this.element.innerHTML = html)
}
}
當(dāng)controller連接元素時(shí),會(huì)根據(jù)data-content-loader-url-value屬性值設(shè)置的url,發(fā)起請(qǐng)求。然后把請(qǐng)求得到的HTML內(nèi)容,賦值給連接元素的innerHTML。
打開(kāi)瀏覽器的開(kāi)發(fā)者工具,點(diǎn)開(kāi)網(wǎng)絡(luò)查看tab頁(yè),然后刷新頁(yè)面。您將看到一個(gè)表示初始頁(yè)面加載的請(qǐng)求,隨后是controller對(duì)messages.html的后續(xù)請(qǐng)求。
我們繼續(xù)優(yōu)化一下controller,隔段時(shí)間就刷新div內(nèi)的內(nèi)容,讓它一直顯示最新的內(nèi)容。
我們使用data-content-loader-refresh-interval-value屬性值來(lái)設(shè)定刷新的時(shí)間間隔,單位是毫秒,
<div data-controller="content-loader"
data-content-loader-url-value="/messages.html"
data-content-loader-refresh-interval-value="5000"></div>
現(xiàn)在我們修改一下controller,檢查間隔,如果間隔值存在,就啟動(dòng)一個(gè)定時(shí)器來(lái)刷新。
在controller里添加一個(gè)static values,然后定義一個(gè)新方法startRefreshing():
export default class extends Controller {
static values = { url: String, refreshInterval: Number }
startRefreshing() {
setInterval(() => {
this.load()
}, this.refreshIntervalValue)
}
// …
}
然后修改connect()方法,如果refreshInterval值存在的話,就調(diào)用startRefreshing()方法。
connect() {
this.load()
if (this.hasRefreshIntervalValue) {
this.startRefreshing()
}
}
刷新頁(yè)面,然后通過(guò)開(kāi)發(fā)者工具,觀察一下,是不是每5秒鐘就會(huì)有一個(gè)新請(qǐng)求。然后可以嘗試修改public/messages.html,所有的改變都會(huì)出現(xiàn)在div內(nèi)。
當(dāng)controller連接元素時(shí),我們啟動(dòng)了定時(shí)器,但是我們沒(méi)有停止它。這意味著,如果我們的controller連接的元素消失的話,controller將在后臺(tái)繼續(xù)發(fā)起HTTP請(qǐng)求。
我們修復(fù)這個(gè)問(wèn)題,修改startRefreshing()方法,保存一個(gè)對(duì)定時(shí)器的引用:
startRefreshing() {
this.refreshTimer = setInterval(() => {
this.load()
}, this.refreshIntervalValue)
}
然后添加一個(gè)對(duì)應(yīng)的stopRefreshing()方法,來(lái)取消定時(shí)器:
stopRefreshing() {
if (this.refreshTimer) {
clearInterval(this.refreshTimer)
}
}
最終,我們告訴Stimulus當(dāng)controller失去連接時(shí),取消定時(shí)器,好,我們添加一個(gè)disconnect()方法:
disconnect() {
this.stopRefreshing()
}
現(xiàn)在我們可以確定,內(nèi)容加載器controller只會(huì)在連接到DOM時(shí)才會(huì)發(fā)出請(qǐng)求。
我們來(lái)看一下最終的controller類:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = { url: String, refreshInterval: Number }
connect() {
this.load()
if (this.hasRefreshIntervalValue) {
this.startRefreshing()
}
}
disconnect() {
this.stopRefreshing()
}
load() {
fetch(this.urlValue)
.then(response => response.text())
.then(html => this.element.innerHTML = html)
}
startRefreshing() {
this.refreshTimer = setInterval(() => {
this.load()
}, this.refreshIntervalValue)
}
stopRefreshing() {
if (this.refreshTimer) {
clearInterval(this.refreshTimer)
}
}
}
本文介紹了如何使用Stimulus生命周期回調(diào)來(lái)獲取和釋放外部資源。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。