You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现屏幕缩小时图片左滑出屏幕而非缩小?

How to Achieve Image Slide-Out on Screen Shrink (No Media Queries)

Hey there! I’ve tinkered with similar effects before, so let’s break down how to replicate that smooth image slide-out behavior you saw—no media queries required. The core idea is using overflow hiding and dynamic CSS transforms tied to viewport width, which lets the image shift automatically as the screen resizes.

Step 1: Basic HTML Structure

First, wrap your image in a container that will handle overflow. This container acts as the "window" that hides parts of the image as it slides out:

<div class="image-slide-container">
  <img src="your-image-path.jpg" alt="Your descriptive alt text here">
</div>

Step 2: Core CSS Styling

This is where the magic happens. We’ll use viewport units (vw) and calc() to create a dynamic translation that shifts the image left as the screen narrows. We also ensure the image keeps its aspect ratio to avoid distortion:

.image-slide-container {
  width: 100%;
  /* Use aspect-ratio to maintain consistent proportions, or set a fixed height */
  aspect-ratio: 16/9; 
  overflow: hidden; /* Critical: hides parts of the image outside the container */
  position: relative; /* Optional, for adding a gradient overlay later */
}

.image-slide-container img {
  width: auto;
  height: 100%;
  /* Keep image proportionate; use object-fit: cover if you want to fill the container */
  object-fit: cover; 
  /* Dynamic translation: shifts left as screen width drops below 1200px */
  /* Adjust 1200px to your desired "full-width" breakpoint */
  transform: translateX(calc((100vw - 1200px) / 2));
  /* Add smooth transition for a polished effect */
  transition: transform 0.3s ease;
}

How It Works

Let’s unpack the transform line:

  • 100vw is the current width of the viewport.
  • When the screen is wider than 1200px, (100vw - 1200px) is positive, so the image shifts right slightly to stay centered in the container.
  • When the screen narrows below 1200px, the value becomes negative, pulling the image left. The overflow-hidden container hides the portion of the image that moves outside its bounds, creating the slide-out effect.

Optional: Add a Gradient Overlay

To make the slide-out look more natural (like a fade instead of a hard cut), add a pseudo-element with a linear gradient to the container:

.image-slide-container::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 150px;
  height: 100%;
  /* Adjust the gradient colors to match your background */
  background: linear-gradient(to right, #ffffff, transparent);
  pointer-events: none; /* Makes sure the overlay doesn’t block clicks on the image */
}

Tweaks for Your Use Case

  • Adjust the 1200px value to match the breakpoint where you want the slide-out to start.
  • If you don’t want the image centered on wide screens, modify the calc() formula (e.g., transform: translateX(calc(100vw - 1200px)); to slide the image fully left as the screen shrinks).
  • Swap aspect-ratio for a fixed height if you need a specific vertical size.

内容的提问来源于stack exchange,提问作者Dog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:40:26