Lädt...

🔧 Resizing a div using CSS resize, but the resizing is done at a fixed dimension (snap grid)


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

TLDR; View the working example on jsfiddle.net

Let's create a solution that resizes a div at fixed 50px increments of its parent container, with smooth transitions, debouncing and ResizeObserver for monitoring.

HTML Setup:

<div class="container">
  <div class="resizable"></div>
</div>

CSS Setup:

.container {
  width: 500px;
  height: 400px;
  border: 2px solid #ccc;
  position: relative;
  overflow: hidden;
}

.resizable {
  width: 250px;
  height: 200px;
  background: #3498db;
  position: absolute;
  resize: both; /* This enables the resizing */
  overflow: auto;
  min-width: 50px;
  min-height: 50px;
  max-width: 100%;
  max-height: 100%;
  transition: all 0.1s ease-out; /* This is important to ensure a smooth drag */
}
  • resize: both enables resizing in both directions
  • transition: all 0.1s ease-out adds smooth transitions to reduce visual glitching
  • min-width/height: 10% and max-width/height: 100% set the boundaries
  • overflow: auto handles content overflow
  • Container has overflow: hidden to prevent content spilling

Javascript Setup:

const resizable = document.querySelector(".resizable")
const container = document.querySelector(".container")

const snapPercent = 20 // Change to fit your needs
const snapPixel = 50 // Change to fit your needs
const usePixels = false // Change this to false to use percentage.

// Function to snap to fixed pixels increments
function snapToGridPixel(size, parentSize) {
  const snappedSize = Math.round(size / snapPixel) * snapPixel
  const value = Math.max(snapPixel, Math.min(snappedSize, parentSize))
  return Math.min(value, parentSize) + "px"
}

// Function to snap to fixed percentage increments
function snapToGridPercentage(size, parentSize) {
  const percentage = (size / parentSize) * 100
  const snappedPercentage = Math.round(percentage / snapPercent) * snapPercent
  return Math.max(snapPercent, Math.min(100, snappedPercentage)) + "%"
}

function snapToGrid(size, parentSize) {
  return usePixels
    ? snapToGridPixel(size, parentSize)
    : snapToGridPercentage(size, parentSize)
}

// Debounce function
function debounce(func, delay) {
  let timeoutId
  return function (...args) {
    clearTimeout(timeoutId)
    timeoutId = setTimeout(() => {
      func.apply(this, args)
    }, delay)
  }
}

// Debounced resize handler
const handleResize = debounce((entries) => {
  for (let entry of entries) {
    const { width, height } = entry.contentRect
    const parentWidth = container.offsetWidth
    const parentHeight = container.offsetHeight

    // Snap to fixed increments
    const newWidth = snapToGrid(width, parentWidth)
    const newHeight = snapToGrid(height, parentHeight)

    // Apply the snapped sizes with max bounds
    resizable.style.width = newWidth
    resizable.style.height = newHeight
  }
}, 100) // 100ms delay

// ResizeObserver with debounced handler
const resizeObserver = new ResizeObserver(handleResize)

// Start observing the resizable element
resizeObserver.observe(resizable)

Let's break down the key components:

JavaScript with ResizeObserver:

  • snapToGrid function calculates the nearest 10% increment
  • Converts current size to percentage of parent
  • Rounds to nearest 20% using Math.round(percentage / 20) * 20
  • Clamps values between 20% and 100%
  • ResizeObserver continuously monitors size changes and applies snapping

The ResizeObserver ensures that whenever the user resizes the div, it immediately snaps to the nearest 10% increment, while the CSS transition makes the snapping motion smooth rather than instantaneous.

Debouncing

Debouncing ensures that a function only runs after a certain period of inactivity. It delays execution until after the triggering event has stopped firing for a specified amount of time.

Why Use Debouncing?

  • Performance: Prevents excessive function calls that could slow down your application
  • Efficiency: Reduces unnecessary computations or API calls
  • User Experience: Ensures actions complete only when the user has finished interacting
...

🔧 Resizing a div using CSS resize, but the resizing is done at a fixed dimension (snap grid)


📈 144.11 Punkte
🔧 Programmierung

🔧 CSS Mask Div with Another Div- Complete Guide


📈 42.79 Punkte
🔧 Programmierung

🔧 CSS Grid: Moving From CSS Frameworks To CSS Grid (2018 and beyond)


📈 40.29 Punkte
🔧 Programmierung

🔧 How to Center a Div Using CSS Grid


📈 38.62 Punkte
🔧 Programmierung

🔧 CSS Grid Handbook – Complete Guide to Grid Containers and Grid Items


📈 37.26 Punkte
🔧 Programmierung

🔧 How to Make Parent Div Activate Styling of Child Div for Hover and Active States


📈 36.16 Punkte
🔧 Programmierung

🐧 div in HTML: Alles Wichtige zur Verwendung des div-Containers


📈 36.16 Punkte
🐧 Server

🔧 CSS: Position fixed div center container


📈 33.94 Punkte
🔧 Programmierung

🔧 How to Build a Grid to List Toggle using CSS Grid and JavaScript


📈 30.75 Punkte
🔧 Programmierung

🔧 Grok 3: AI Thông Minh Nhất Thế Giới


📈 28.59 Punkte
🔧 Programmierung

🕵️ Kèo Thẻ Phạt Vip66 Là Gì? 3 Lối Đánh Kèo Chậm Mà Chắc


📈 28.59 Punkte
🕵️ Reverse Engineering

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 28.59 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 28.59 Punkte
🔧 Programmierung

🔧 Seven quickest ways to center your div using CSS


📈 28.4 Punkte
🔧 Programmierung

🔧 Seven quickest ways to center your div using CSS


📈 28.4 Punkte
🔧 Programmierung

📰 How to resize and snap windows with swipe gestures on macOS


📈 27.07 Punkte
🖥️ Betriebssysteme

📰 How to resize and snap windows with swipe gestures on macOS


📈 27.07 Punkte
🖥️ Betriebssysteme

🍏 How to resize and snap windows with swipe gestures on macOS


📈 27.07 Punkte
🍏 iOS / Mac OS

🔧 CSS Grid: Nested Grid Layouts


📈 27.05 Punkte
🔧 Programmierung

🔧 `grid` with reveal + resize on `hover`


📈 26.73 Punkte
🔧 Programmierung

🔧 Introduction of CSS, What is CSS, Why we use CSS and How CSS describe the HTML elements


📈 26.49 Punkte
🔧 Programmierung

🔧 2024:How to Center a Div in CSS


📈 24.7 Punkte
🔧 Programmierung

🔧 Berbagai Cara Efektif untuk Memposisikan Div di Tengah dengan CSS


📈 24.7 Punkte
🔧 Programmierung

🔧 Berbagai Cara Efektif untuk Memposisikan Div di Tengah dengan CSS


📈 24.7 Punkte
🔧 Programmierung

🔧 How to center a Div in HTML and CSS?


📈 24.7 Punkte
🔧 Programmierung

🔧 How to Access Direct Children of a Div in Tailwind CSS v3


📈 24.7 Punkte
🔧 Programmierung

🔧 How to Vertically Align Content with Tailwind CSS Across a Full-Screen Div


📈 24.7 Punkte
🔧 Programmierung

🔧 TidyCoder day night loading only css/HTML, and single div


📈 24.7 Punkte
🔧 Programmierung

🔧 3 Different Ways to Center A Div in CSS


📈 24.7 Punkte
🔧 Programmierung

🔧 How to Center a Div : CSS Tips and Tricks


📈 24.7 Punkte
🔧 Programmierung