整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

不使用 Javascript 也可以和浏览器进行交互?

tmx 是一个库,它允许你直接从 HTML 访问现代浏览器功能,而不是使用 javascript。

要理解 htmx,首先让我们看一下 HTML 中的 a 标签:

<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>

这个标记会告诉浏览器:当用户单击此链接时,向 /blog 发出 HTTP GET 请求并将响应内容加载到浏览器窗口中。

然后我们再看下面的 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:当用户单击此按钮时,向 /clicked 发出 HTTP POST 请求并使用响应中的内容将元素替换为 id 为 parent-div 的 DOM。

Htmx 将 HTML 的核心思想进行了扩展,为 HTML 语言提供了更多可能性:

  • 现在任何元素,不仅仅是超链接和表单,都可以发出 HTTP 请求。
  • 现在任何事件,不仅仅是点击或表单提交,都可以触发请求。
  • 现在可以使用任何 HTTP verb,而不仅仅是GET 和POST。
  • 现在任何元素,不仅仅是整个window 对象,都可以成为请求更新的目标。

请注意,当你使用 htmx 时,在服务器端你通常会使用 HTML 而非 JSON 进行响应。这会让你使用原始 Web 编程模型,使用超文本作为应用程序状态的引擎,甚至你也不需要真正理解这个概念。

另外如果你愿意,可以在使用 htmx 时使用 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 是一个无依赖、面向浏览器的 JavaScript 库。这意味着使用它就像在文档头部添加一个 <script> 标记一样简单,无需复杂的构建步骤或系统。

通过 CDN

使用 htmx 的最快方法是通过 CDN 加载它。你可以简单地将其添加到你的 head 标签中即可:

<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>

npm

对于 npm 风格的构建系统,同样你可以通过 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>

安装后,你需要使用适当的工具来使用 node_modules/htmx.org/dist/htmx.js(或 .min.js),例如你可以将 htmx 与一些扩展和特定于项目的代码捆绑在一起。

如果你使用 webpack 来管理你的 javascript:

  • 通过你最喜欢的包管理器(如 npm 或 yarn)安装 htmx。
  • 在你的index.js 文件 import 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;">import 'htmx.org';</pre>

如果要使用全局 htmx 变量(推荐),则需要将其注入到 window 作用域中:

  • 创建一个定制的 JS 文件。
  • 在index.js 文件中导入该文件。
<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>
  • 最后重新打包即可

AJAX

htmx 的核心是一组允许你直接从 HTML 发出 AJAX 请求的属性:

  • hx-get:向指定的 URL 发出 GET 请求。
  • hx-post:向指定的 URL 发出 POST 请求。
  • hx-put:向指定的 URL 发出 PUT 请求。
  • hx-patch:向指定的 URL 发出 PATCH 请求。
  • hx-delete:向指定的 URL 发出 DELETE 请求。

这些属性都需要一个 URL 来向其发出 AJAX 请求,当元素被触发时,该元素将向给定的 URL 发出指定类型的请求:

<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>

这会告诉浏览器:当用户点击此 div 时,向 /messages 发出 PUT 请求并将响应加载到 div。

触发请求

默认情况下,AJAX 请求由元素的 自然 事件触发:

  • input、textarea​ 和select​ 在change 事件上触发。
  • form 在提交事件上触发。
  • 其他元素都由click 事件触发。

如果你想要不同的行为,可以使用 hx-trigger 属性来指定哪个事件将触发请求。

比如下面的一段代码表示在鼠标进入时触发到 /mouse_entered 的 POST 请求:

<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/ 了解更多。

示例

下面我们用几个示例来简单说明下 htmx 是如何使用的。

点击加载数据

这个例子展示了如何在数据表格中实现点击加载下一页,关键是最后一行:

<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>

该行包含一个按钮,该按钮将用下一页结果替换整行(其中将包含一个用于加载下一页结果的按钮)。

当点击 Load More Agents... 按钮后会加载一页数据附加到表格中去。

延迟加载

这个例子展示了如何在页面上延迟加载元素。我们从如下所示的初始状态开始:

<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>

当我们加载图表时,它会显示一个进度指示器,然后通过 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>

该示例的效果就是先显示一个加载的指示器,然后加载一张图片出来,就是通常的延迟加载的效果。

Git 仓库:https://github.com/bigskysoftware/htmx。

来源: https://developer.51cto.com/article/714198.html

秘CSS3:打造视觉盛宴——深度解析渐变、阴影与遮罩技术

随着Web技术的不断演进,CSS3以其强大的视觉表现力,赋予网页设计无限可能。本文将深入剖析CSS3中的三大视觉魔法工具——渐变、阴影与遮罩技术,通过详尽的理论讲解和丰富的实例演示,助您掌握这些技巧,打造出令人眼前一亮的网页视觉盛宴。

一、魅力渐变:色彩的艺术流动

1. 线性渐变:平滑过渡,简约而不简单

css
background: linear-gradient(to right, #ff6b6b, #ff9595);

上述代码创建了一个从左至右,由#ff6b6b渐变到#ff9595的线性渐变背景。您可以调整方向(如`to bottom`、`45deg`等)、添加更多颜色停止点来丰富渐变效果。

2. 径向渐变:聚焦视觉中心,营造立体感

css
background: radial-gradient(circle at center, #f7f7f7, #dcdcdc);

此例中,我们创建了一个以元素中心为圆心,从#f7f7f7渐变到#dcdcdc的圆形径向渐变背景。通过调整形状(如`ellipse`)、大小(如`closest-side`)和位置(如`top left`),可以灵活定制径向渐变样式。

二、立体阴影:赋予元素生命与空间感

1. 盒子阴影(Box Shadow):轻松实现三维效果

css
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3);

上述代码为元素添加了一个向右下偏移2px、模糊半径为8px、颜色为rgba(0, 0, 0, 0.3)的阴影。理解盒阴影的基本参数(水平偏移、垂直偏移、模糊半径、扩散半径、颜色)并灵活运用,即可创造出丰富的阴影效果。

2. 文本阴影(Text Shadow):让文字跃然纸上

css
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.½), -1px -1px 2px rgba(255, 255, 255, 0.5);

此处为文本设置了两个阴影:一个向右下偏移、颜色较深的阴影,以及一个向左上偏移、颜色较浅的阴影,形成微妙的浮雕效果。通过叠加多个阴影、调整参数,您可以创作出各种独特的文本样式。

三、神奇遮罩:掌控元素可见度与透明度

1. CSS Mask:精细裁剪,展现独特视界

css
mask-image: linear-gradient(to right, transparent 0%, black 50%, transparent 100%);

该代码为元素应用了一个从左至右的线性渐变遮罩,使得元素左侧和右侧各有一半区域透明。您还可以使用`mask-mode`、`mask-repeat`、`mask-position`等属性进一步调整遮罩行为。

2. CSS Clip Path:创意裁剪,打破常规布局

css
clip-path: polygon(0 0, 100% 0, .png);

上述代码使用多边形裁剪路径,将元素顶部裁剪成尖角形状。您还可以使用椭圆、圆形、内切/外切矩形等多种路径类型,甚至借助SVG路径实现更为复杂的裁剪效果。

四、实战案例:融合三大技术,打造视觉盛宴

案例一:动态渐变按钮

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变量、伪类和动画,创建一个点击时背景渐变颜色动态变化的按钮:

案例二:悬浮卡片与阴影交互

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);
}

结合盒子阴影与:hover伪类,实现鼠标悬停时卡片阴影增强的交互效果:

案例三:遮罩叠加文字特效

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%);
}

利用遮罩与多重背景,创造出文字在图片上淡入淡出的特效:

结语

CSS3的渐变、阴影与遮罩技术,如同网页设计的调色板、光影魔术师和剪刀手,赋予网页视觉表现无尽的可能性。通过深入理解并熟练运用这些技术,您将能打造出既美观又富有创意的网页界面,为用户带来极致的视觉体验。持续探索、实践与创新,您的每一个作品都将成为Web世界中的一道独特风景。

1- CSS 过渡的目的是什么?举个例子。

CSS 过渡允许您在指定的持续时间内平滑地对 CSS 属性的变化进行动画处理。通过定义转换属性和值,您可以在不同状态之间创建平滑的转换。

这是一个例子:

.element {  transition: background-color 0.3s ease;}
.element:hover {  background-color: red;}

32-如何使用 CSS 变换创建旋转动画效果?

CSS 变换允许您操纵元素的位置、大小和旋转。要创建旋转动画效果,可以将变换属性与旋转函数结合使用,并使用动画属性指定动画持续时间。

这是一个例子:

@keyframes rotate {  0% {    transform: rotate(0deg);  }  100% {    transform: rotate(360deg);  }}
.element {  animation: rotate 2s linear infinite;}

33-如何使用 CSS 动画创建淡入效果?

CSS 动画允许您创建复杂且自定义的动画。要创建淡入效果,您可以定义一个动画,在指定的持续时间内将元素的不透明度逐渐从 0 更改为 1。

这是一个例子:

@keyframes fadeIn {  0% {    opacity: 0;  }  100% {    opacity: 1;  }}
.element {  animation: fadeIn 1s ease-in;}

34-如何使用 CSS 过渡创建下拉菜单?

CSS 过渡可用于为下拉菜单创建平滑的动画。通过将过渡应用到下拉容器的 max-height 或 opacity 属性,您可以创建无缝的打开和关闭效果。

这是一个例子:

.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 创建视差滚动效果?

视差滚动是一种通过以不同速度移动不同元素来创建深度错觉的技术。您可以通过应用 CSS 属性(例如背景附件、背景位置和变换)的组合来实现此效果。这是一个例子:

.section {  background-image: url('background.jpg');  background-size: cover;  background-attachment: fixed;  background-position: center;  transform: translateZ(0);}