Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-img-viewr

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-img-viewr

vue图片查看组件(vue image viewer component)

  • 1.0.7
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

vue-img-viewr

npm package

❗ vue图片查看组件(vue image viewer component),1.x版本为vue@2.x组件

❗ 如需在vue@3.x中使用,请使用 vue-img-viewr@2.x, Github v2.x地址 github 2.x
yarn add vue-img-viewr
npm i vue-img-viewr -S

示例

demo展示

vue-img-viewr

用法

# 安装依赖
yarn add vue-img-viewr@^1.0.7
# or
npm i vue-img-viewr@^1.0.7 -S

使用,SPA,非SSR

/**
 * 一、推荐🔥全局js方式引入
 */
import Vue from 'vue'
import { showImages } from 'vue-img-viewr'
import 'vue-img-viewr/styles/index.css'

Vue.prototype.$showImages = showImages

/** 使用 **/
this.$showImages({
  urls: this.urls,
  index
})

/**
 * 二、按需引入使用
 */
import ImgViewr, { showImages } from 'vue-img-viewr'
import 'vue-img-viewr/styles/index.css'

export default {
  // 注册组件
  components: { ImgViewr },
  data () {
    return {
      urls: [
        'https://s.gravatar.com/avatar/221f86a573320174bad7a62886a6d4b4?size=100&default=retro',
        'https://s.gravatar.com/avatar/221f86a573320174bad7a62886a6d4b4',
        'https://static.npmjs.com/attachments/ck3uwf5d872zb8874c3ayi1pj-icon-pro-wombat-3x.png'
      ],
      index: 0,
      visible: false
    }
  },
  methods: {
    // 通过组件方式
    showImagesByComponent (index) {
      this.visible = true
      this.index = index
    },
    onShow (isShow) {
      console.log(isShow)
    },
    changeHandle (i) {
      console.log(`current image index: ${i}`)
    },
    showImagesByJs (index) {
      // 通过js方式显示
      showImages({
        urls: this.urls,
        index,
        onShow: this.onShow,
        onClose: () => {
          console.log('close with js')
          return true
        },
      })
    }
  }
}

服务端渲染(SSR)中使用,以Nuxtjs为例

/**
 * 引用
 */
// @/plugins/vue-img-viewr.js
import Vue from 'vue'
import ImgViewr, { showImages } from 'vue-img-viewr'
import 'vue-img-viewr/styles/index.css'

export default () => {
  // 全局方法
  Vue.prototype.$showImages = showImages
  // 注册组件
  Vue.component(ImgViewr.name, ImgViewr)
}

// nuxt.config.js
plugins: [
  { src: '@/plugins/vue-img-viewr', ssr: false }
]

/**
 * 使用
 */
export default {
  data () {
    return {
      urls: [
        'https://s.gravatar.com/avatar/221f86a573320174bad7a62886a6d4b4?size=100&default=retro',
        'https://s.gravatar.com/avatar/221f86a573320174bad7a62886a6d4b4',
        'https://static.npmjs.com/attachments/ck3uwf5d872zb8874c3ayi1pj-icon-pro-wombat-3x.png'
      ],
      index: 0,
      visible: false
    }
  },
  methods: {
    // 通过组件方式
    showImagesByComponent (index) {
      this.visible = true
      this.index = index
    },
    onShow (isShow) {
      console.log(isShow)
    },
    changeHandle (i) {
      console.log(`current image index: ${i}`)
    },
    showImagesByJs (index) {
      // 通过js方式显示
      this.$showImages({
        urls: this.urls,
        index,
        onShow: this.onShow,
        onClose: () => {
          console.log('close with js')
          return true
        },
      })
    }
  }
}

示例组件使用

<!-- 使用组件 -->
<div class="demo">
  <h3>通过组件方式</h3>
  <div class="imgs">
    <div v-for="(url, i) in urls" class="img" :key="`c_${i}`" @click="() => showImagesByComponent(i)">
      <img :src="url" />
    </div>
  </div>
  <ImgViewr :visible="visible" :urls="urls" :initialIndex="index" :on-show="onShow" :on-switch="changeHandle" :on-close="() => (visible = false)" />
  <h3>通过js方法调用</h3>
  <div class="imgs">
    <div v-for="(url, i) in urls" class="img" :key="`j_${i}`" @click="() => showImagesByJs(i)">
      <img :src="url" />
    </div>
  </div>
</div>

示例样式

/* 示例样式 */
.imgs {
  display: flex;
}
.img {
  width: 150px;
  height: 100px;
  border: 1px solid #EEE;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 20px;
}
.img img {
  max-width: 100%;
  max-height: 100%;
  cursor: pointer;
}

参数

Attributes
参数说明类型可选值默认值
urls需要展示的图片url数组(必须参数)array<string>
visible是否显示组件(仅限于通过组件方式参数)booleantrue / false
initialIndex初始显示的图片索引(仅限于通过组件方式参数)number0
index显示的图片索引(仅限于通过js方法调用参数)number0
onSwitch图片切换函数 Function(index)function
onClose关闭函数(通过js方法调用,返回true才会关闭;组件方式可以直接设置visible为false)function
onShow组件显示关闭监听 Function(isShow) (主要用于js方式调用)function
zIndex层级number3000
lockScroll是否在查看图片时将 body 滚动锁定booleantrue / falsetrue
closeOnClickMask点击蒙层关闭booleantrue / falsetrue

Keywords

FAQs

Package last updated on 22 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc