Socket
Socket
Sign inDemoInstall

img-previewer

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    img-previewer

A light wight javascript image viewing plugin with smooth animation


Version published
Weekly downloads
26
increased by73.33%
Maintainers
1
Install size
170 kB
Created
Weekly downloads
 

Readme

Source

Img-previewer Js

GitHub license GitHub stars npm bundle size GitHub package.json version (subfolder of monorepo)

Lightweight and powerful javascript image preview plug-in, silky animation allows you to elegantly preview the images in your website. Out of the box, you don't need extra configuration (by default) or change the page html code structure, you can easily enable the plugin in any type of website and upgrade your user experience

These functions are provided:

  1. Silky, interruptible transition animation
  2. Use mouse wheel to zoom picture
  3. Icon drag picture
  4. Previous & Next
  5. Shortcut key support
  6. Support for mobile gestures (zoom in with two fingers)
  7. Multi-language internationalization support
  8. Picture loading monitor

Other languages: English, 简体中文.

tips: For performance reasons, the mobile terminal does not do swiper

Example

Preview

how to use

NPM

npm i img-previewer
# or
yarn add img-previewer

CDN

<script src="https://cdn.jsdelivr.net/npm/img-previewer@2.1.6/dist/img-previewer.min.js"></script>

Enable

//js
import ImgPreviewer from'img-previewer'
//css
import'img-previewer/dist/index.css'

const imgPreviewer = new ImgPreviewer(css selector,{options...})

Property list

TypeDescriptionDefault Value
fillRationumberThe proportion of the image that fills the preview area0.9(90%)
dataUrlKeystringThe key of the image address valuesrc
triggerEventstringtrigger eventclick
imageZoomobjectZoom image configuration{min: 0.1,max: 5,step: 0.1}
styleobjectStyle configuration{modalOpacity: 0.6,headerOpacity: 0,zIndex: 99}
i18nobjecttooltips International configurationnull
bubblingLevelnumberBubble to detect whether the parent element of the image is hidden by the css style0

Optional values for triggerEvent are: click and dblclick

bubblingLevel Description

You should try to use this property when you notice an abnormal image hide animation. Because when the image or the parent element of the image is hidden by some CSS styles, it cannot be detected through the js api, so you need to pass in the correct upward lookup level according to the actual situation to help the plug-in complete the correct hiding animation. As shown below, the correct bubblingLevel is at least 3

for performance considerations, it is not recommended to fill in this attribute value at will

<div style="opacity:0"> <!-- 3 -->
	<div> <!-- 2 -->
		<img src="" alt="" /> <!-- 1 -->
	</div>
</div>

Notice: Currently detecting that an element or parent element is hidden by a css style only supports the following styles:

  • opacity: 0;
  • height: 0;
  • width: 0;
  • visibility: hidden;

options.imageZoom

DescriptionDefault value
minMinimum zoom ratio0.1(10%)
maxMaximum zoom ratio5(500%)
stepThe change ratio of the scroll wheel each time0.1

options.style

DescriptionDefault value
modalOpacityPreview area mask transparency0.6
headerOpacityToolbar transparency0
zIndexLevel of plug-in rendering99

options.i18n

Simplified Chinese and English are supported by default, others need to be configured by themselves

Description
RESETReset
ROTATE_LEFTRotate Left
ROTATE_RIGHTRotate right
CLOSEClose preview
NEXTNext
PREVPrevious

api methods

Method nameDescription
update()update image els
getTotalIndex()get total image el numbers
show(index:number)show index image
next()goto next
prev()goto prev

hot key

ButtonDescription
EscClose preview
<=Previous
=>Next

Update picture

Some dynamically updated picture lists use

const imgPreviewer = new ImgPreviewer('body')
// Called after the image is rendered on the page
imgPreviewer.update()

play slideshow

let timer = null
function play() {
	timer && clearInterval(timer)
	let index = 0
	a.show(index)
	timer = setInterval(() => {
		if (index < a.getTotalIndex()) {
			index++
		} else {
			index = 0
		}
		a.show(index)
	}, 2000)
}
play()

Keywords

FAQs

Last updated on 14 May 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc