Tailwind CSS - 触摸行为



Tailwind CSS 触摸行为是一组预定义的类,用于控制如何在网页上处理触摸交互,例如滚动和缩放。

Tailwind CSS 触摸行为类

下面是用于控制触摸行为的 Tailwind CSS 触摸行为类的列表。

类名 CSS 属性
touch-auto touch-action: auto;
touch-none touch-action: none;
touch-pan-x touch-action: pan-x;
touch-pan-left touch-action: pan-left;
touch-pan-right touch-action: pan-right;
touch-pan-y touch-action: pan-y;
touch-pan-up touch-action: pan-up;
touch-pan-down touch-action: pan-down;
touch-pinch-zoom touch-action: pinch-zoom;
touch-manipulation touch-action: manipulation;

Tailwind CSS 触摸行为类的功能

  • touch-auto: 允许正常的触摸交互,例如点击和滚动。
  • touch-none: 禁用所有触摸交互,因此用户无法在此元素上使用触摸操作。
  • touch-pan-x: 允许水平滑动,允许用户左右移动。
  • touch-pan-left: 将触摸限制为仅向左移动或滚动。
  • touch-pan-right: 将触摸限制为仅向右移动或滚动。
  • touch-pan-y: 允许垂直滑动,允许用户上下移动。
  • touch-pan-up: 将触摸限制为仅向上移动或滚动。
  • touch-pan-down: 将触摸限制为仅向下移动或滚动。
  • touch-pinch-zoom: 允许通过用两个手指捏合来放大和缩小。
  • touch-manipulation: 允许触摸操作,例如移动和缩放,限制较少。

Tailwind CSS 触摸行为类示例

以下是 Tailwind CSS 触摸行为类的示例,这些示例演示了如何在网页上控制各种触摸行为,例如滚动、捏合和平移。

理解 Tailwind CSS 中的触摸行为

此示例显示了 Tailwind CSS 中的 **touch-auto** 和 **touch-none** 类如何影响触摸交互。第一张图片允许滚动和缩放手势,而第二张图片对触摸手势没有响应。

示例

  
<!DOCTYPE html>
<html lang="en">
<head> 
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="p-4">
    <h1 class="text-2xl font-bold mb-6">
        Tailwind CSS Touch Action
    </h1>
    <h3 class="underline font-bold mb-4">
        Try scrolling these images on a touchscreen.
    </h3>
    <div class="grid grid-cols-2 gap-4"> 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: auto</h3>
            <div class="w-full h-60 overflow-auto touch-auto">
                <img class="w-[150%] max-w-none h-auto" 
                    src="https://picsum.photos/600/400?random" 
                    alt="Placeholder Image" />
            </div>
        </div>
 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: none</h3>
            <div class="w-full h-60 overflow-auto touch-none">
                <img class="w-[150%] max-w-none h-auto" 
                    src="https://picsum.photos/600/400?random" 
                    alt="Placeholder Image" />
            </div>
        </div>
    </div>
</body> 

</html>

使用触摸行为进行滚动方向控制

此示例显示 **touch-pan-x** 类如何允许水平滚动(左右),同时限制垂直滚动,而 **touch-pan-y** 类允许垂直滚动(上下),同时限制水平滚动,因此您可以在触摸屏上仅沿一个方向移动图像。

示例

<!DOCTYPE html>
<html lang="en">
<head> 
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="p-4">
    <h1 class="text-2xl font-bold mb-6">
        Tailwind CSS Touch Action
    </h1>
    <h3 class="underline font-bold mb-4">
        Try scrolling these images on a touchscreen.
    </h3>
     <div class="grid grid-cols-2 gap-4"> 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: pan-x</h3>
            <div class="w-full h-60 overflow-auto touch-pan-up">
                <img class="w-[200%] max-w-none h-auto" 
                    src="https://picsum.photos/600/400?random" 
                    alt="Placeholder Image"/>
            </div>
        </div>
 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: pan-y</h3>
            <div class="w-full h-60 overflow-auto touch-pan-down">
                <img class="w-[200%] max-w-none h-auto" 
                    src="https://picsum.photos/600/400?random" 
                    alt="Placeholder Image" />
            </div>
        </div>
    </div>
</body>

</html>

Tailwind CSS 向上平移和向下平移

此示例显示 **touch-pan-up** 类如何允许用户向上滚动图像,而 **touch-pan-down** 类允许向下滚动,显示触摸交互如何被限制在触摸屏上的特定方向。

示例

<!DOCTYPE html>
<html lang="en">
<head>  
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="p-4">
    <h1 class="text-2xl font-bold mb-6">
        Tailwind CSS Touch Action
    </h1>
    <h3 class="underline font-bold mb-4">
       Try scrolling these images on a touchscreen
    </h3>
    <div class="grid grid-cols-2 gap-4">
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: pan-up</h3>
            <div class="w-full h-60 overflow-auto touch-pan-up">
                <img class="w-[200%] max-w-none h-auto" 
                            src="https://picsum.photos/600/400?random" 
                            alt="Placeholder Image" />
            </div>
            <p class="mt-2 text-sm">Scroll up</p>
        </div> 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: pan-down</h3>
            <div class="w-full h-60 overflow-auto touch-pan-down">
                <img class="w-[200%] max-w-none h-auto" 
                            src="https://picsum.photos/600/400?random" 
                            alt="Placeholder Image" />
            </div>
            <p class="mt-2 text-sm">Scroll down</p>
        </div>
    </div>
</body>

</html>

使用 Tailwind CSS 进行捏合缩放和默认触摸行为

此示例演示了 **touch-pinch-zoom** 类如何启用使用捏合手势进行放大和缩小,而 **touch-manipulation** 类允许所有标准触摸交互,包括滚动和缩放。

示例

<!DOCTYPE html>
<html lang="en">
<head>  
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="p-4">
    <h1 class="text-2xl font-bold mb-6">
        Tailwind CSS Touch Action
    </h1>
    <h3 class="underline font-bold mb-4">
        Try scrolling these images on a touchscreen.
    </h3>
    <div class="grid grid-cols-2 gap-4"> 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: pinch-zoom</h3>
            <div class="w-full h-60 overflow-auto touch-pinch-zoom">
                <img class="w-[150%] max-w-none h-auto" 
                    src="https://picsum.photos/600/400?random" 
                    alt="Placeholder Image" />
            </div>
            <p class="mt-2">Zoom with pinch.</p>
        </div>
 
        <div class="border border-red-300 p-2 relative">
            <h3 class="font-bold underline mb-2">Touch Action: manipulation</h3>
            <div class="w-full h-60 overflow-auto touch-manipulation">
                <img class="w-[150%] max-w-none h-auto" 
                            src="https://picsum.photos/600/400?random" 
                            alt="Placeholder Image" />
            </div>
            <p class="mt-2">Default touch gestures.</p>
        </div>
    </div>
    <p class="mt-4 text-center">Use the <strong>touch-*</strong>
        classes to control touch interactions.
    </p>
</body>

</html>
广告