Skip to content

Drag Limits

Intermediate

Constrain dragging in specific directions with maxDragX and maxDragY.

Demo

Source
vue
<script lang="ts" setup>
import { ref } from 'vue'
import { FlashCards } from 'vue3-flashcards'
import LimitCard from './LimitCard.vue'
import '../assets/index.css'

const cards = ref([
  { text: 'Mathematics', subtitle: 'Basic algebra and geometry', color: 'from-blue-500 to-blue-600' },
  { text: 'Science', subtitle: 'Physics and chemistry fundamentals', color: 'from-green-500 to-green-600' },
  { text: 'Literature', subtitle: 'Classic novels and poetry', color: 'from-purple-500 to-purple-600' },
])

const limitY = ref(true)
const limitX = ref(true)
</script>

<template>
  <div class="w-full flex flex-col justify-center items-center py-20 space-y-8">
    <div class="flex space-x-6 p-4 bg-gray-100 dark:bg-gray-800 rounded-lg">
      <label class="flex items-center space-x-2">
        <input v-model="limitY" type="checkbox" class="toggle toggle-primary">
        <span class="text-gray-700 dark:text-gray-300">Limit Y (0px)</span>
      </label>
      <label class="flex items-center space-x-2">
        <input v-model="limitX" type="checkbox" class="toggle toggle-primary">
        <span class="text-gray-700 dark:text-gray-300">Limit X (200px)</span>
      </label>
    </div>

    <div class="max-w-sm w-full">
      <FlashCards
        :items="cards"
        :max-drag-x="limitX ? 200 : null"
        :max-drag-y="limitY ? 0 : null"
        #="{ item }"
      >
        <LimitCard :item="item" />
      </FlashCards>
    </div>
  </div>
</template>
vue
<script setup lang="ts">
interface CardItem {
  text: string
  subtitle: string
  color: string
}

defineProps<{
  item: CardItem
}>()
</script>

<template>
  <div class="relative overflow-hidden rounded-2xl shadow-2xl h-64 bg-white dark:bg-gray-800 transform transition-all duration-300 hover:scale-105">
    <!-- Gradient header -->
    <div :class="`bg-gradient-to-r ${item.color} h-20 relative`">
      <div class="absolute inset-0 bg-black/10" />
      <div class="absolute bottom-3 left-6 text-white">
        <div class="w-8 h-8 bg-white/20 rounded-full flex items-center justify-center">
          <div class="w-4 h-4 bg-white rounded-full" />
        </div>
      </div>
    </div>

    <!-- Card content -->
    <div class="p-6 flex flex-col justify-center items-start h-44">
      <h2 class="text-2xl font-bold text-gray-800 dark:text-gray-100 mb-2">
        {{ item.text }}
      </h2>
      <p class="text-gray-600 dark:text-gray-300 text-sm leading-relaxed">
        {{ item.subtitle }}
      </p>

      <!-- Card footer -->
      <div class="absolute bottom-4 right-6 flex space-x-2">
        <div class="w-2 h-2 bg-gray-300 rounded-full" />
        <div class="w-2 h-2 bg-gray-300 rounded-full" />
        <div class="w-2 h-2 bg-blue-500 rounded-full" />
      </div>
    </div>
  </div>
</template>

Key Concepts

  • maxDragX: Limit horizontal dragging (0 = no horizontal drag)
  • maxDragY: Limit vertical dragging (0 = no vertical drag)
  • Combine with swipeDirection to control allowed swipes
  • Useful for: vertical-only cards with controlled horizontal movement

Released under the MIT License.