Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

photoswipe-dynamic-caption-plugin

> 编程语言
Open source

A dynamic caption plugin for PhotoSwipe v5. Automatically positions the caption aside or below the image.

99 stars0 likes5 views
WebsiteGitHub

About

A dynamic caption plugin for PhotoSwipe v5. Automatically positions the caption aside or below the image.

Dynamic caption plugin for PhotoSwipe v5

**[> Plugin demo { return slide.data.element.querySelector('img').getAttribute('alt'); }


#### `type: 'auto'`

Position type of the caption can be 'auto', 'below', or 'aside'.

- 'below' - caption will always be placed below the image
- 'aside' - caption will always be placed on the right side of the image
- 'auto'  - the plugin will try to automatically determine the best position (depending on available space)

#### `mobileLayoutBreakpoint: 600`

Maximum window width at which mobile layout should be used, or a function that should return true if mobile layout should be used. For example:
  
```js
mobileLayoutBreakpoint: (pswp, captionPlugin) => {
  return (window.innerWidth < 750);
}

…

css
.pswp__dynamic-caption--aside {
  max-width: 300px;
  padding: 20px 15px 20px 20px;
  margin-top: 70px;
}
.pswp__dynamic-caption--below {
  max-width: 700px;
  padding: 15px 0 0;
}
.pswp__dynamic-caption--mobile {
  background: rgba(0, 0, 0, 0.5);
  padding: 10px 15px;
}

How 'auto' positioning works

  • Check if there is more horizontal or vertical free space around the image.
  • If there is more free vertical space:
    • Set caption width to the width of the image
    • Add pswp__dynamic-caption--below class, so the size can also be adjusted via CSS.
    • Measure caption height.
    • Check if the caption will fit without any adjustments to the image position.
      • If it does - just show the caption below the image.
      • If it doesn't - reduce the pan area height by the height of the caption.
  • If there is more horizontal space:
    • Add pswp__dynamic-caption--aside class, so the size can be adjusted via CSS.
    • Measure caption width.
    • Check if caption will fit on the right side without any adjustments of image position.
      • If it does - just show the caption aside from the image.
      • If it doesn't - reduce the pan area width by the width of the caption.

If mobileLayoutBreakpoint requirements are met:

  • Measure caption height when it occupies 100% of width.
  • Reduce pan area height to fit the caption below the image.
  • Check the amount of free horizontal space after the adjustment.
  • If there is too much horizontal space (mobileCaptionOverlapRatio) - just overlap the caption and keep the image at the default position.

Changelog

1.2.0

  • Requires PhotoSwipe 5.3.0.
  • Caption now moves with the slide when dragged (instead of fading in and out).
  • Each slide now has a separate DOM element (before there was only a single caption element that changed content).
  • No longer uses temporary caption to measure size.
  • You may now access caption and its data via dynamicCaption property of a slide. For example pswp.currSlide.dynamicCaption.

1.1.0

  • No longer adjusts main image padding. If you need to dynamically change padding based on the screen size - use PhotoSwipe option paddingFn (introduced in 5.1.61)
  • Caption receives class pswp__dynamic-caption--on-hor-edge when it's on horizontal edge (x position is less than threshold). Added option horizontalEdgeThreshold to control this.
  • Reworked mobile layout. Now the caption tries to not overlap the main image if there is an empty space below. Added option mobileCaptionOverlapRatio to control this.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptphotoswipe

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言