css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              #app
  .card
    h1.card-title Swipe to Zoom Out
    p.card-text This demo shows how to implement "swipe to dismiss zoomed image". If you swipe the image on dialog vertically, the dialog will dismiss itself.
    .card-image
      image-dialog(
        thumb="https://via.placeholder.com/315x200"
        full="https://via.placeholder.com/945x600",
        :full-width="945",
        :full-height="600")
      
script#image-dialog(type="text/x-template")
  .image-dialog
    button.image-dialog-trigger(type="button" @click="showDialog")
      img.image-dialog-thumb(ref="thumb", :src="thumb")
    transition(name="dialog" @enter="enter" @leave="leave" @after-enter="teardown" @after-leave="teardown")
      .image-dialog-background(
        v-show="appearedDialog" 
        ref="dialog",
        :class="{ cancelling: cancelling }",
        :style="{ 'background-color': 'rgba(255, 255, 255, ' + backgroundOpacity + ')' }"
        @mousedown.prevent="startDrag"
        @mousemove.prevent="doDrag"
        @mouseup.prevent="endDrag"
        @mouseleave.prevent="cancelDrag"
        @touchstart.prevent="startDrag"
        @touchmove.prevent="doDrag"
        @touchend.prevent="endDrag"
        @touchcancel.prevent="cancelDrag"
      )
        img.image-dialog-animate(
          ref="animate", 
          :class="{ loading: !loaded }", 
          :src="loaded ? full : thumb")
        img.image-dialog-full(
          ref="full",
          :class="{ cancelling: cancelling }",
          :src="appearedDialog && full", 
          :width="fullWidth", 
          :height="fullHeight",
          :style="{ transform: 'translateY(' + dragDeltaY + 'px)' }"
          @transitionend="onTransitionEndFull"
          @load="onLoadFull")
            
          
!
            
              $transition-image: cubic-bezier(0,.7,.3,.95);

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  font-family: "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  background-color: #9c7bb4;
}

.card {
  margin: 50px auto;
  padding: 20px;
  width: 355px;
  background-color: #fff;
  box-shadow: 5px 5px 30px rgba(65, 57, 115, 0.4);
  
  &-title {
    color: #101029;
    font-size: 22px;
  }
  
  &-text {
    margin-top: 8px;
    line-height: 1.3;
  }
  
  &-image {
    margin-top: 10px;
  }
}

.image-dialog {
  &-trigger {
    margin: 0;
    padding: 0;
    background: none;
    border: none;
    cursor: pointer;
  }
  
  &-thumb {
    vertical-align: top;
  }
  
  &-background {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    cursor: pointer;
    
    &.cancelling {
      transition: background $transition-image;
    }
  }
  
  &-animate {
    display: none;
    position: absolute;
    transform-origin: left top;
    
    &.loading {
      display: block;
    }
  }
  
  &-full {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    margin: auto;
    width: auto;
    height: auto;
    max-height: 100%;
    max-width: 100%;
    
    &.cancelling {
      transition: transform 300ms $transition-image;
    }
  }
}

.dialog {
  &-enter-active,
  &-leave-active {
    transition: background-color 300ms $transition-image;
  }
  
  &-enter,
  &-leave-to {
    background-color: rgba(255, 255, 255, 0) !important;
  }
  
  &-enter-active .image-dialog-animate,
  &-leave-active .image-dialog-animate {
    display: block;
    transition: transform 300ms $transition-image;
  }
  
  &-enter-active .image-dialog-full,
  &-leave-active .image-dialog-full {
    visibility: hidden;
  }
}
            
          
!
            
              Vue.component('image-dialog', {
  template: '#image-dialog',
  
  props: {
    thumb: String,
    full: String,
    fullWidth: Number,
    fullHeight: Number
  },
  
  data () {
    return {
      loaded: false,
      appearedDialog: false,
      dragging: false,
      dragStartY: 0,
      draggingY: 0,
      cancelling: false
    }
  },
  
  computed: {
    dragThreshold: () => 170,
    
    dragDeltaY () {
      return this.draggingY - this.dragStartY
    },
    
    backgroundOpacity () {
      return (1 - Math.min(Math.abs(this.dragDeltaY) / this.dragThreshold, 1)) * 0.8 + 0.1
    }
  },
  
  methods: {
    getY (event) {
      return typeof event.pageY === 'number'
        ? event.pageY
        : event.changedTouches[0].pageY
    },
    
    showDialog () {
      this.appearedDialog = true
    },
    
    hideDialog () {
      this.appearedDialog = false
    },
    
    enter () {
      this.animateImage(
        this.$refs.thumb.getBoundingClientRect(),
        this.getFullImageBound
      )
    },
    
    leave () {
      this.animateImage(
        this.$refs.full.getBoundingClientRect(),
        () => this.$refs.thumb.getBoundingClientRect()
      )
    },
    
    teardown () {
      this.draggingY = this.dragStartY = 0
    },
    
    startDrag (event) {
      this.dragging = true
      this.dragStartY = this.draggingY = this.getY(event)
    },
    
    doDrag (event) {
      if (this.dragging) {
        this.draggingY = this.getY(event)
      }
    },
    
    endDrag (event) {
      if (!this.dragging) {
        return
      }
      
      if (this.dragDeltaY === 0) {
        this.dragging = false
        return
      }
      
      if (Math.abs(this.dragDeltaY) < this.dragThreshold) {
        this.cancelDrag()
      } else {
        this.hideDialog()
      }
      this.dragging = false
    },
    
    cancelDrag () {
      if (this.dragging) {
        this.dragging = false
        this.cancelling = true
        this.draggingY = this.dragStartY = 0
      }
    },
    
    onLoadFull () {
      this.loaded = true
    },
    
    onTransitionEndFull () {
      this.cancelling = false
    },
  
    animateImage (start, getDest) {
      this.setStart(start)
      
      this.$nextTick(() => {
        const dest = getDest()
        this.setDestination(start, dest)
      })
    },
    
    getFullImageBound () {
      const bound = this.$refs.dialog.getBoundingClientRect()
      const { width, height } = bound
      
      const scaleY = this.fullHeight / height
      const scaleX = this.fullWidth / width
      const scale = Math.max(Math.max(scaleY, scaleX), 1)
      
      const destHeight = this.fullHeight / scale
      const destWidth = this.fullWidth / scale

      return {
        top: (height - destHeight) / 2,
        left: (width - destWidth) / 2,
        width: destWidth,
        height: destHeight
      }
    },
    
    setStart (start, dest) {
      const el = this.$refs.animate
      el.style.left = start.left + 'px'
      el.style.top = start.top + 'px'
      el.style.width = start.width + 'px'
      el.style.height = start.height + 'px'
      el.style.transitionDuration = '0s'
      el.style.transform = ''
    },
    
    setDestination (start, dest) {
      const el = this.$refs.animate
      el.style.transitionDuration = ''
      
      const translate = `translate(${dest.left - start.left}px, ${dest.top - start.top}px)`
      const scale = `scale(${dest.width / start.width}, ${dest.height / start.height})`
      el.style.transform = `${translate} ${scale}`
    }
  }
})

new Vue({
  el: '#app'
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console