js响应式弹出层图片全屏查看器

所属分类: 网页特效-弹出层    2024-01-31 11:46:56

js响应式弹出层图片全屏查看器 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

js响应式弹出层图片全屏查看器(共3个文件)

    • script.js
    • index.html
    • style.css

使用方法

// Inspired by Medium image zoom animation

let isOpen = false

let overlay

function createOverlay() {
  overlay = document.createElement('zoomable-image-overlay')
  document.body.appendChild(overlay)
}

class ZoomableImageOverlay extends HTMLElement {
  connectedCallback() {
    const style = document.createElement('style')
    style.textContent = ZoomableImageOverlay.style

    const shadow = this.attachShadow({ mode: 'open' })
    shadow.appendChild(style)
  }

  show() {
    this.setAttribute('shown', '')
  }

  hide() {
    this.removeAttribute('shown')
  }

  static get style() {
    return `
      :host {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 1;
        background: rgba(255, 255, 255, 0.8);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        width: 100%;
        height: 100%;
        opacity: 0;
        pointer-events: none;
        transition: 0.2s ease opacity;
      }

      :host([shown]) {
        cursor: zoom-out;
        opacity: 1;
        pointer-events: all;
      }
    `
  }
}

class ZoomableImage extends HTMLElement {
  isOpen = false
  connectedCallback() {
    this.image = this.querySelector('img')
    overlay.addEventListener('click', this.close.bind(this))
    window.addEventListener('scroll', this.close.bind(this))

    const style = document.createElement('style')
    style.textContent = ZoomableImage.style

    const shadow = this.attachShadow({ mode: 'open' })
    shadow.appendChild(style)
    shadow.appendChild(this.image)

    this.addEventListener('click', this.toggle.bind(this))
    window.addEventListener('resize', this.updateTransform.bind(this))
  }

  toggle() {
    if (isOpen) {
      this.close()
      return
    }
    isOpen = true
    this.isOpen = true
    overlay.show()
    this.setAttribute('open', '')
    this.image.style.zIndex = '2'
    this.originalImageRect = this.image.getBoundingClientRect()
    this.updateTransform()
  }

  updateTransform() {
    if (!this.isOpen) return
    const wrapperRect = this.getBoundingClientRect()
    const imageMaxWidth = this.image.naturalWidth
    const imageMaxHeight = this.image.naturalHeight
    const fullWidth = Math.min(window.innerWidth, imageMaxWidth)
    const fullHeight = Math.min(window.innerHeight, imageMaxHeight)
    const scaleX = fullWidth / this.originalImageRect.width
    const scaleY = fullHeight / this.originalImageRect.height
    const scale = Math.min(scaleX, scaleY)
    const translateX =
      -wrapperRect.left +
      (window.innerWidth - this.originalImageRect.width * scale) / 2
    const translateY =
      -wrapperRect.top +
      (window.innerHeight - this.originalImageRect.height * scale) / 2
    this.image.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`
  }

  close() {
    if (!this.isOpen) return
    this.isOpen = false
    isOpen = false
    this.removeAttribute('open')
    const onTransitionEnd = () => {
      this.image.style.zIndex = ''
      this.image.removeEventListener('transitionend', onTransitionEnd)
    }
    this.image.addEventListener('transitionend', onTransitionEnd)
    this.image.style.transform = 'none'
    overlay.hide()
  }

  static get style() {
    return `
      :host {
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;
        cursor: zoom-in;
      }
      :host([open]) {
        cursor: zoom-out;
      }
      img {
        width: 100%;
        transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1);
        transform-origin: left top;
      }
    `
  }
}

createOverlay()

customElements.define('zoomable-image', ZoomableImage)
customElements.define('zoomable-image-overlay', ZoomableImageOverlay)

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 弹出层

html5基于MUI框架手机城市弹出层选择代码

包含多种不同的选择类型,选择日期、选择省市区、选择省市、选择省份。
  弹出层
 126  

原生js半透明点击弹出提示层特效

可自动弹出或鼠标点击弹出,可自定义设置提示层关闭时间,非常实用!
  弹出层
 131  

jquery页面右下角Whatsapp浮动聊天窗口特效

一款绿色风格在线聊天弹出层插件,点击列表可跳转到外部链接,非常实用!
  弹出层
 452  

jquery支持AJAX交互的弹出层插件

Vintage Popup是一款响应式弹出层插件,用于在支持静态和动态(AJAX)模式内容,可自定义提示窗口。
  弹出层
 116  

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论