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
tmx 是一個(gè)庫,它允許你直接從 HTML 訪問現(xiàn)代瀏覽器功能,而不是使用 javascript。
要理解 htmx,首先讓我們看一下 HTML 中的 a 標(biāo)簽:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a href="/blog">Blog</a></pre>
這個(gè)標(biāo)記會(huì)告訴瀏覽器:當(dāng)用戶單擊此鏈接時(shí),向 /blog 發(fā)出 HTTP GET 請(qǐng)求并將響應(yīng)內(nèi)容加載到瀏覽器窗口中。
然后我們?cè)倏聪旅娴?HTML:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><button hx-post="/clicked"
hx-trigger="click"
hx-target="#parent-div"
hx-swap="outerHTML"
>
Click Me!
</button></pre>
這告訴 htmx:當(dāng)用戶單擊此按鈕時(shí),向 /clicked 發(fā)出 HTTP POST 請(qǐng)求并使用響應(yīng)中的內(nèi)容將元素替換為 id 為 parent-div 的 DOM。
Htmx 將 HTML 的核心思想進(jìn)行了擴(kuò)展,為 HTML 語言提供了更多可能性:
請(qǐng)注意,當(dāng)你使用 htmx 時(shí),在服務(wù)器端你通常會(huì)使用 HTML 而非 JSON 進(jìn)行響應(yīng)。這會(huì)讓你使用原始 Web 編程模型,使用超文本作為應(yīng)用程序狀態(tài)的引擎,甚至你也不需要真正理解這個(gè)概念。
另外如果你愿意,可以在使用 htmx 時(shí)使用 data- 前綴:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><a data-hx-post="/click">Click Me!</a></pre>
Htmx 是一個(gè)無依賴、面向?yàn)g覽器的 JavaScript 庫。這意味著使用它就像在文檔頭部添加一個(gè) <script> 標(biāo)記一樣簡(jiǎn)單,無需復(fù)雜的構(gòu)建步驟或系統(tǒng)。
使用 htmx 的最快方法是通過 CDN 加載它。你可以簡(jiǎn)單地將其添加到你的 head 標(biāo)簽中即可:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><script src="https://unpkg.com/htmx.org@1.8.0" integrity="sha384-cZuAZ+ZbwkNRnrKi05G/fjBX+azI9DNOkNYysZ0I/X5ZFgsmMiBXgDZof30F5ofc" crossorigin="anonymous"></script></pre>
對(duì)于 npm 風(fēng)格的構(gòu)建系統(tǒng),同樣你可以通過 npm 安裝 htmx:
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">npm install htmx.org</pre>
安裝后,你需要使用適當(dāng)?shù)墓ぞ邅硎褂?node_modules/htmx.org/dist/htmx.js(或 .min.js),例如你可以將 htmx 與一些擴(kuò)展和特定于項(xiàng)目的代碼捆綁在一起。
如果你使用 webpack 來管理你的 javascript:
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">import 'htmx.org';</pre>
如果要使用全局 htmx 變量(推薦),則需要將其注入到 window 作用域中:
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">import 'path/to/my_custom.js';</pre>
<pre class="prettyprint hljs javascript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">window.htmx = require('htmx.org');</pre>
htmx 的核心是一組允許你直接從 HTML 發(fā)出 AJAX 請(qǐng)求的屬性:
這些屬性都需要一個(gè) URL 來向其發(fā)出 AJAX 請(qǐng)求,當(dāng)元素被觸發(fā)時(shí),該元素將向給定的 URL 發(fā)出指定類型的請(qǐng)求:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div hx-put="/messages">
Put To Messages
</div></pre>
這會(huì)告訴瀏覽器:當(dāng)用戶點(diǎn)擊此 div 時(shí),向 /messages 發(fā)出 PUT 請(qǐng)求并將響應(yīng)加載到 div。
觸發(fā)請(qǐng)求
默認(rèn)情況下,AJAX 請(qǐng)求由元素的 自然 事件觸發(fā):
如果你想要不同的行為,可以使用 hx-trigger 屬性來指定哪個(gè)事件將觸發(fā)請(qǐng)求。
比如下面的一段代碼表示在鼠標(biāo)進(jìn)入時(shí)觸發(fā)到 /mouse_entered 的 POST 請(qǐng)求:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div hx-post="/mouse_entered" hx-trigger="mouseenter">
[Here Mouse, Mouse!]
</div></pre>
HTMX 還有很多使用的方式,可以前往官方文檔 https://htmx.org/docs/ 了解更多。
下面我們用幾個(gè)示例來簡(jiǎn)單說明下 htmx 是如何使用的。
這個(gè)例子展示了如何在數(shù)據(jù)表格中實(shí)現(xiàn)點(diǎn)擊加載下一頁,關(guān)鍵是最后一行:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><tr id="replaceMe">
<td colspan="3">
<button class='btn' hx-get="/contacts/?page=2"
hx-target="#replaceMe"
hx-swap="outerHTML">
Load More Agents... <img class="htmx-indicator" src="/img/bars.svg">
</button>
</td>
</tr></pre>
該行包含一個(gè)按鈕,該按鈕將用下一頁結(jié)果替換整行(其中將包含一個(gè)用于加載下一頁結(jié)果的按鈕)。
當(dāng)點(diǎn)擊 Load More Agents... 按鈕后會(huì)加載一頁數(shù)據(jù)附加到表格中去。
這個(gè)例子展示了如何在頁面上延遲加載元素。我們從如下所示的初始狀態(tài)開始:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div hx-get="/graph" hx-trigger="load">
<img alt="Result loading..." class="htmx-indicator" width="150" src="/img/bars.svg"/>
</div></pre>
當(dāng)我們加載圖表時(shí),它會(huì)顯示一個(gè)進(jìn)度指示器,然后通過 CSS 過渡加載圖表并逐漸淡入視圖:
<pre class="prettyprint hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">.htmx-settling img {
opacity: 0;
}
img {
transition: opacity 300ms ease-in;
}</pre>
該示例的效果就是先顯示一個(gè)加載的指示器,然后加載一張圖片出來,就是通常的延遲加載的效果。
Git 倉庫:https://github.com/bigskysoftware/htmx。
來源: https://developer.51cto.com/article/714198.html
隨著Web技術(shù)的不斷演進(jìn),CSS3以其強(qiáng)大的視覺表現(xiàn)力,賦予網(wǎng)頁設(shè)計(jì)無限可能。本文將深入剖析CSS3中的三大視覺魔法工具——漸變、陰影與遮罩技術(shù),通過詳盡的理論講解和豐富的實(shí)例演示,助您掌握這些技巧,打造出令人眼前一亮的網(wǎng)頁視覺盛宴。
1. 線性漸變:平滑過渡,簡(jiǎn)約而不簡(jiǎn)單
css
background: linear-gradient(to right, #ff6b6b, #ff9595);
上述代碼創(chuàng)建了一個(gè)從左至右,由#ff6b6b漸變到#ff9595的線性漸變背景。您可以調(diào)整方向(如`to bottom`、`45deg`等)、添加更多顏色停止點(diǎn)來豐富漸變效果。
2. 徑向漸變:聚焦視覺中心,營造立體感
css
background: radial-gradient(circle at center, #f7f7f7, #dcdcdc);
此例中,我們創(chuàng)建了一個(gè)以元素中心為圓心,從#f7f7f7漸變到#dcdcdc的圓形徑向漸變背景。通過調(diào)整形狀(如`ellipse`)、大小(如`closest-side`)和位置(如`top left`),可以靈活定制徑向漸變樣式。
1. 盒子陰影(Box Shadow):輕松實(shí)現(xiàn)三維效果
css
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3);
上述代碼為元素添加了一個(gè)向右下偏移2px、模糊半徑為8px、顏色為rgba(0, 0, 0, 0.3)的陰影。理解盒陰影的基本參數(shù)(水平偏移、垂直偏移、模糊半徑、擴(kuò)散半徑、顏色)并靈活運(yùn)用,即可創(chuàng)造出豐富的陰影效果。
2. 文本陰影(Text Shadow):讓文字躍然紙上
css
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.?), -1px -1px 2px rgba(255, 255, 255, 0.5);
此處為文本設(shè)置了兩個(gè)陰影:一個(gè)向右下偏移、顏色較深的陰影,以及一個(gè)向左上偏移、顏色較淺的陰影,形成微妙的浮雕效果。通過疊加多個(gè)陰影、調(diào)整參數(shù),您可以創(chuàng)作出各種獨(dú)特的文本樣式。
1. CSS Mask:精細(xì)裁剪,展現(xiàn)獨(dú)特視界
css
mask-image: linear-gradient(to right, transparent 0%, black 50%, transparent 100%);
該代碼為元素應(yīng)用了一個(gè)從左至右的線性漸變遮罩,使得元素左側(cè)和右側(cè)各有一半?yún)^(qū)域透明。您還可以使用`mask-mode`、`mask-repeat`、`mask-position`等屬性進(jìn)一步調(diào)整遮罩行為。
2. CSS Clip Path:創(chuàng)意裁剪,打破常規(guī)布局
css
clip-path: polygon(0 0, 100% 0, .png);
上述代碼使用多邊形裁剪路徑,將元素頂部裁剪成尖角形狀。您還可以使用橢圓、圓形、內(nèi)切/外切矩形等多種路徑類型,甚至借助SVG路徑實(shí)現(xiàn)更為復(fù)雜的裁剪效果。
案例一:動(dòng)態(tài)漸變按鈕
css
/* 定義CSS變量 */
:root {
--start-color: #ff6b6b;
--end-color: #ff9595;
}
.button {
background: linear-gradient(to right, var(--start-color), var(--end-color));
transition: background 0.3s ease-in-out;
}
.button:hover,
.button:focus {
--start-color: #ff9595;
--end-color: #ff6b6b;
}
利用CSS變量、偽類和動(dòng)畫,創(chuàng)建一個(gè)點(diǎn)擊時(shí)背景漸變顏色動(dòng)態(tài)變化的按鈕:
案例二:懸浮卡片與陰影交互
css
.card {
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3);
}
.card:hover {
box-shadow: 4px 4px 16px rgba(0, 0, 0, 0.5);
}
結(jié)合盒子陰影與:hover偽類,實(shí)現(xiàn)鼠標(biāo)懸停時(shí)卡片陰影增強(qiáng)的交互效果:
案例三:遮罩疊加文字特效
css
.image-overlay {
background-image: url('image.jpg'), linear-gradient(to bottom, transparent, black);
background-blend-mode: multiply;
mask-image: linear-gradient(to bottom, transparent 0%, black 100%);
-webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 100%);
}
利用遮罩與多重背景,創(chuàng)造出文字在圖片上淡入淡出的特效:
結(jié)語
CSS3的漸變、陰影與遮罩技術(shù),如同網(wǎng)頁設(shè)計(jì)的調(diào)色板、光影魔術(shù)師和剪刀手,賦予網(wǎng)頁視覺表現(xiàn)無盡的可能性。通過深入理解并熟練運(yùn)用這些技術(shù),您將能打造出既美觀又富有創(chuàng)意的網(wǎng)頁界面,為用戶帶來極致的視覺體驗(yàn)。持續(xù)探索、實(shí)踐與創(chuàng)新,您的每一個(gè)作品都將成為Web世界中的一道獨(dú)特風(fēng)景。
1- CSS 過渡的目的是什么?舉個(gè)例子。
CSS 過渡允許您在指定的持續(xù)時(shí)間內(nèi)平滑地對(duì) CSS 屬性的變化進(jìn)行動(dòng)畫處理。通過定義轉(zhuǎn)換屬性和值,您可以在不同狀態(tài)之間創(chuàng)建平滑的轉(zhuǎn)換。
這是一個(gè)例子:
.element { transition: background-color 0.3s ease;}
.element:hover { background-color: red;}
32-如何使用 CSS 變換創(chuàng)建旋轉(zhuǎn)動(dòng)畫效果?
CSS 變換允許您操縱元素的位置、大小和旋轉(zhuǎn)。要?jiǎng)?chuàng)建旋轉(zhuǎn)動(dòng)畫效果,可以將變換屬性與旋轉(zhuǎn)函數(shù)結(jié)合使用,并使用動(dòng)畫屬性指定動(dòng)畫持續(xù)時(shí)間。
這是一個(gè)例子:
@keyframes rotate { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); }}
.element { animation: rotate 2s linear infinite;}
33-如何使用 CSS 動(dòng)畫創(chuàng)建淡入效果?
CSS 動(dòng)畫允許您創(chuàng)建復(fù)雜且自定義的動(dòng)畫。要?jiǎng)?chuàng)建淡入效果,您可以定義一個(gè)動(dòng)畫,在指定的持續(xù)時(shí)間內(nèi)將元素的不透明度逐漸從 0 更改為 1。
這是一個(gè)例子:
@keyframes fadeIn { 0% { opacity: 0; } 100% { opacity: 1; }}
.element { animation: fadeIn 1s ease-in;}
34-如何使用 CSS 過渡創(chuàng)建下拉菜單?
CSS 過渡可用于為下拉菜單創(chuàng)建平滑的動(dòng)畫。通過將過渡應(yīng)用到下拉容器的 max-height 或 opacity 屬性,您可以創(chuàng)建無縫的打開和關(guān)閉效果。
這是一個(gè)例子:
.dropdown { max-height: 0; opacity: 0; transition: max-height 0.3s ease, opacity 0.3s ease;}
.dropdown.open { max-height: 300px; opacity: 1;}
35-如何使用 CSS 創(chuàng)建視差滾動(dòng)效果?
視差滾動(dòng)是一種通過以不同速度移動(dòng)不同元素來創(chuàng)建深度錯(cuò)覺的技術(shù)。您可以通過應(yīng)用 CSS 屬性(例如背景附件、背景位置和變換)的組合來實(shí)現(xiàn)此效果。這是一個(gè)例子:
.section { background-image: url('background.jpg'); background-size: cover; background-attachment: fixed; background-position: center; transform: translateZ(0);}
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。