HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="root">
</div>
.react-image-viewer {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
.button-group {
float: right;
margin-right: 4rem;
.button {
position: relative;
display: block;
cursor: pointer;
text-align: center;
float: left;
cursor: pointer;
transition: all .4s ease;
&:hover {
opacity: .8;
}
}
}
.prev-button, .next-button {
position: fixed;
line-height: 80vh;
background: none;
border: none;
top: 10vh;
padding: 0;
color: #fff;
cursor: pointer;
opacity: .4;
transition: all .4s ease;
&:hover {
opacity: .8;
}
}
.prev-button {
left: 0;
}
.next-button {
right: 0;
}
.image-wrapper {
position: relative;
width: 92vw;
height: 92vh;
margin: 4vh 4vw;
overflow: hidden;
}
.image-outer {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
text-align: center;
transition: all .35s ease;
-webkit-transition: all .35s ease;
cursor: move;
&.dragging {
transition: all 0s ease;
-webkit-transition: all 0s ease;
}
&:before {
content: "";
display: inline-block;
height: 50%;
width: 1px;
margin-right: -1px;
}
.image {
display: inline-block;
vertical-align: middle;
max-width: 100%;
max-height: 100%;
width: auto !important;
height: auto !important;
background-size: contain!important;
background-position: center!important;
background-repeat: no-repeat!important;
transform: scale3d(1, 1, 1);
-webkit-transform: scale3d(1, 1, 1);
transition: all .4s ease;
-webkit-transition: all .4s ease;
animation: image-fade-in .4s ease;
-webkit-animation: image-fade-in .4s ease;
}
&.zoom-0 {
cursor: default;
}
&.zoom-1 {
.image {
transform: scale3d(1.5, 1.5, 1);
-webkit-transform: scale3d(1.5, 1.5, 1);
}
}
&.zoom-2 {
.image {
transform: scale3d(2, 2, 1);
-webkit-transform: scale3d(2, 2, 1);
}
}
&.zoom-3 {
.image {
transform: scale3d(2.5, 2.5, 1);
-webkit-transform: scale3d(2.5, 2.5, 1);
}
}
&.zoom-4 {
.image {
transform: scale3d(3, 3, 1);
-webkit-transform: scale3d(3, 3, 1);
}
}
}
.tool-bar {
height: 4rem;
width: 100vw;
position: fixed;
left: 0;
top: 0;
padding-right: 4rem;
line-height: 4rem;
opacity: .5;
background: rgba(0, 0, 0, 0);
transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
min-width: 16rem;
overflow: hidden;
&:hover {
opacity: 1;
background: rgba(0, 0, 0, .7);
}
.index-indicator, .caption {
float: left;
color: rgba(255,255,255, .6);
font-size: 1rem;
font-weight: lighter;
padding-left: 1.4rem;
line-height: 4rem;
vertical-align: middle;
margin: 0;
}
.caption {
max-width: 60vw;
overflow: hidden;
}
}
.image-info {
position: absolute;
bottom: 2rem;
left: 0;
width: 100%;
text-align: center;
line-height: 1.4rem;
font-size: .9rem;
font-weight: lighter;
color: rgba(255,255,255,.6);
}
.indicators {
position: fixed;
left: 0;
bottom: 1rem;
width: 100vw;
height: 3rem;
min-width: 30rem;
overflow: hidden;
display: flex;
flex-flow: row wrap;
justify-content: center;
align-items: center;
.indicators-item {
position: relative;
width: 4rem;
max-width: 10vw;
height: 3rem;
margin: 0 .3rem;
float: left;
line-height: 3rem;
opacity: .25;
cursor: pointer;
transition: all .4s ease;
-webkit-transition: all .4s ease;
&:first-child, &:last-child {
&:after {
position: absolute;
content: "";
display: inline-block;
height: .4rem;
width: .4rem;
border-radius: 4rem;
background: RGBA(44, 183, 245, 1);
top: 1.4rem;
opacity: 0;
transform: scale(0);
-webkit-transform: scale(0);
transition: all .6s ease;
-webkit-transition: all .6s ease;
}
&.invisible:after {
opacity: 1;
transform: scale(1);
-webkit-transform: scale(1);
}
}
&:first-child:after {
left: -1rem;
}
&:last-child:after {
right: -1rem;
}
&.invisible {
width: 0;
margin: 0;
}
&:before {
content: "";
display: inline-block;
height: .3rem;
width: 100%;
border-radius: 5rem;
background: RGBA(44, 183, 245, 1);
}
&.active, &:hover {
opacity: .85!important;
}
&.preview {
border: 1px solid #eee;
opacity: .5;
box-sizing: border-box;
&.active, &:hover {
border: 1px solid RGBA(44, 183, 245, 1);
}
&:first-child, &:last-child {
&:after {
content: '';
display: inline-block;
height: 2rem;
width: .1rem;
background: rgba(255, 255, 255, .8);
position: absolute;
top: .5rem;
opacity: 0;
transform: scaleY(0);
-webkit-transform: scaleY(0);
transition: all .6s ease;
-webkit-transition: all .6s ease;
}
&.invisible:after {
opacity: 1;
transform: scaleY(1);
-webkit-transform: scaleY(1);
}
}
&.invisible {
border: none!important;
}
&:before {
display: none;
}
.image {
width: 100%;
height: 100%;
background-size: cover!important;
background-position: center!important;
overflow: hidden;
}
}
}
}
.spinner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
text-align: center;
&:before {
content: "";
display: inline-block;
height: 50%;
width: 1px;
margin-right: -1px;
}
}
}
.react-image-viewer {
.direction-control-button {
.button {
.bar {
width: 4rem;
height: 4rem;
display: inline-block;
vertical-align: middle;
position: relative;
&:before, &:after {
content: '';
position: absolute;
height: 2rem;
width: .12rem;
left: 1rem;
top: 0;
background-color: #fff;
transform-origin: 0 100%;
}
&:before {
transform: rotate(45deg);
}
&:after {
transform: rotate(135deg);
}
}
}
.next-button .bar {
transform: rotate(180deg);
}
}
.button-group {
.button {
width: 2rem;
height: 2rem;
text-align: center;
margin: .86rem 1rem;
opacity: .5;
border: .14rem solid #fff;
border-radius: 50%;
&:before, &:after {
content: '';
display: block;
position: absolute;
height: 1.6rem;
width: .12rem;
top: .2rem;
left: .94rem;
background-color: #fff;
border-radius: 2rem;
transform-origin: center;
transform: rotate(90deg);
}
}
.zoom-in:after {
transform: rotate(0deg);
}
}
.bounce {
width: 2.4rem;
height: 2.4rem;
display: inline-block;
position: relative;
&:before, &:after {
content: '';
display: block;
position: absolute;
width: 100%;
height: 100%;
opacity: .5;
border-radius: 50%;
background-color: RGBA(44, 183, 245, 1.00);
-webkit-animation: bounce 2.0s infinite ease-in-out;
animation: bounce 2.0s infinite ease-in-out;
}
}
.bounce:after {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
}
.image-gallery {
display: flex;
height: 100vh;
flex-flow: row wrap;
justify-content: space-around;
align-items: center;
}
.image-item {
width: 10rem;
height: 10rem;
background: #fff;
overflow: hidden;
cursor: pointer;
border: 1rem solid #fff;
box-shadow: 0 0 .5rem rgba(0,0,0,.4);
}
.image-inner {
width: 100%;
height: 100%;
background-size: cover!important;
background-position: center!important;
transition: all .3s ease;
transform: scale(1);
&:hover {
transform: scale(1.2);
}
}
.modal {
position: fixed;
z-index: 10000;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, .95);
.close-button {
position: absolute;
width: 2.4rem;
height: 2.4rem;
right: .8rem;
top: .8rem;
cursor: pointer;
transition: all .4s ease;
opacity: .5;
&:before, &:after {
content: '';
display: block;
position: absolute;
height: 2.4rem;
width: .12rem;
top: 0;
right: 1.13rem;
background-color: #fff;
border-radius: 2rem;
transform-origin: center;
}
&:before {
transform: rotate(45deg);
}
&:after {
transform: rotate(-45deg);
}
&:hover {
opacity: .8;
}
}
}
@-webkit-keyframes bounce {
0%, 100% { -webkit-transform: scale(0.0) }
50% { -webkit-transform: scale(1.0) }
}
@keyframes image-fade-in {
0% {
opacity: .75;
}
100% {
opacity: 1;
}
}
const data: any = [
{
src: 'https://unsplash.it/800/300?image=1',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/1300?image=22',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/1800/1300?image=13',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/600?image=8',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/200/300?image=15',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/300?image=6',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/300?image=1',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/1300?image=22',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/1800/1300?image=13',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/600?image=8',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/200/300?image=15',
title: 'title',
content: 'content'
},
{
src: 'https://unsplash.it/800/300?image=6',
title: 'title',
content: 'content'
}
];
// ------------
const {
Component,
PropTypes
} = React;
const ZOOM_LEVEL = {
MIN: 0,
MAX: 4
};
const VISIBLE_INDICATORS_COUNT = 8;
const KEY_CODE = {
LEFT: 37,
RIGTH: 39
};
const OFFSET_DEFAULT = {
x: 0,
y: 0
};
interface Image {
src: string;
// Caption => {title} - {content}
title?: string;
content?: string;
}
interface Offset {
x: number,
y: number
}
interface ImageWrapperProps {
image: Image;
showIndex: boolean;
index?: string;
}
class ImageWrapper extends Component<ImageWrapperProps, any> {
private imageOuter: HTMLElement;
private image: HTMLElement;
private draggable: boolean;
private src: any;
private clientOffset: Offset;
private offsetRange: Offset;
private mounted: boolean;
constructor(props: ImageWrapperProps, context: any) {
super(props, context)
this.state = {
loading: false,
onload: false,
zoom: 0,
offset: OFFSET_DEFAULT as Offset
}
this.draggable = false;
this.offsetRange = OFFSET_DEFAULT;
this.clientOffset = {
x: undefined,
y: undefined
};
}
private loadImage(src: string) {
this.state.loading = true;
this.setState(this.state);
this.src = new Image();
this.src.src = src;
this.src.onload = () => {
if(!this.src) return;
this.state.loading = false;
this.state.onload = true;
this.setState(this.state);
}
this.src.onerror = () => {
if(!this.src) return;
this.state.loading = false;
this.state.onload = false;
this.setState(this.state);
}
}
private resetOffset() {
this.state.offset = OFFSET_DEFAULT;
this.setState(this.state);
}
private setOffsetRange() {
const zoom: number = this.state.zoom;
const dx: number = this.image.scrollWidth * (1 + zoom / 2) - this.imageOuter.clientWidth;
const dy: number = this.image.scrollHeight * (1 + zoom / 2) - this.imageOuter.clientHeight;
this.offsetRange = {
x: Math.max(0, dx / 2),
y: Math.max(0, dy / 2)
}
}
private zoomIn() {
if(!this.state.onload) return;
this.state.zoom = Math.min(this.state.zoom + 1, ZOOM_LEVEL.MAX);
this.setState(this.state);
this.setOffsetRange();
}
private zoomOut() {
if(!this.state.onload) return;
this.state.zoom = Math.max(0, this.state.zoom - 1);
this.setState(this.state);
this.resetOffset();
this.setOffsetRange();
}
private onMoveStart(e: any) {
if(!this.offsetRange.x && !this.offsetRange.y) {
return;
}
this.clientOffset = {
x: e.clientX,
y: e.clientY
}
this.draggable = true;
}
private onMove(e: any) {
if(!e.clientX && !e.clientY || !this.draggable) {
return;
}
const offset: Offset = {
x: e.clientX - this.clientOffset.x,
y: e.clientY - this.clientOffset.y,
};
this.clientOffset = {
x: e.clientX,
y: e.clientY
};
this.state.offset = {
x: this.state.offset.x + offset.x,
y: this.state.offset.y + offset.y,
}
this.setState(this.state);
}
private onMoveEnd(e: any) {
if(!this.mounted) return;
this.draggable = false;
const offset: Offset = {
x: Math.abs(this.state.offset.x),
y: Math.abs(this.state.offset.y)
}
if(Math.abs(this.state.offset.x) >= this.offsetRange.x) {
this.state.offset.x = this.state.offset.x < 0 ? Math.min(0, -(this.offsetRange.x)) : Math.max(0, this.offsetRange.x);
this.setState(this.state);
}
if(Math.abs(this.state.offset.y) >= this.offsetRange.y) {
this.state.offset.y = this.state.offset.y < 0 ? Math.min(0, -(this.offsetRange.y)) : Math.max(0, this.offsetRange.y);
this.setState(this.state);
}
}
componentWillReceiveProps(nextProps: any) {
if(this.props.image.src != nextProps.image.src) {
this.resetOffset();
this.loadImage(nextProps.image.src);
this.setState({
zoom: 0
});
}
}
componentDidMount() {
this.mounted = true;
this.loadImage(this.props.image.src);
window.addEventListener('resize', this.setOffsetRange.bind(this));
document.documentElement.addEventListener("mouseup", this.onMoveEnd.bind(this));
}
componentWillUnmount() {
this.mounted = false;
if(!!this.src) {
this.src = undefined;
}
window.removeEventListener('resize', this.setOffsetRange.bind(this));
document.documentElement.removeEventListener("mouseup", this.onMoveEnd.bind(this));
}
render() {
const {
image,
index,
showIndex
} = this.props;
const {
offset,
zoom,
loading
} = this.state;
const value: string = `translate3d(${offset.x}px, ${offset.y}px, 0px)`;
const imageCls: string = `zoom-${zoom} image-outer ${this.draggable ? 'dragging' : ''}`;
const caption: any = (
<p className="caption">
{image.title ? <span className="title">{image.title}</span> : null}
{image.title && image.content ? <span>{` - `}</span> : null}
{image.title ? <span className="content">{image.content}</span> : null}
</p>
);
return (
<div className="image-wrapper">
<div
style={{ transform: value}}
ref={(component: any) => this.imageOuter = component}
className={imageCls}>
{loading ? (
<div className="spinner">
<div className="bounce"></div>
</div>
) : <img
className="image"
ref={(component: any) => this.image = component}
src={image.src}
alt={image.title || ''}
draggable={false}
onDragStart={(e: any) => e.preventDefault()}
onMouseMove={this.onMove.bind(this)}
onMouseDown={this.onMoveStart.bind(this)}
onMouseUp={this.onMoveEnd.bind(this)}/>}
</div>
<div className="tool-bar">
{showIndex && <div className="index-indicator">{index}</div>}
{caption}
<div className="button-group">
<div className="zoom-out button"
onClick={this.zoomOut.bind(this)}>
</div>
<div className="zoom-in button"
onClick={this.zoomIn.bind(this)}>
</div>
</div>
</div>
</div>
)
}
}
interface ImageViewerProps {
images: Image[];
activeIndex?: number;
showIndex?: boolean;
showPreview?: boolean;
prefixCls?: string;
}
class ImageViewer extends Component<ImageViewerProps, any> {
private container: HTMLElement;
private mounted: boolean;
static defaultProps = {
prefixCls: 'react-image-viewer',
className: '',
showIndex: true,
showPreview: true,
activeIndex: 0,
};
static propTypes = {
prefixCls: PropTypes.string,
className: PropTypes.string,
showIndex: PropTypes.bool,
showPreview: PropTypes.bool,
activeIndex: PropTypes.number,
};
constructor(props: ImageViewerProps) {
super(props);
this.state = {
activeIndex: this.props.activeIndex
}
}
private renderIndicators(list: Image[]) {
const activeIndex: number = this.state.activeIndex;
const ret: number = Math.round(VISIBLE_INDICATORS_COUNT / 2);
const length: number = list.length;
return list.map((item: Image, index: number) => {
const isActive: boolean = activeIndex === index;
const itemInvisible: boolean = length > VISIBLE_INDICATORS_COUNT && (index < Math.min(length - VISIBLE_INDICATORS_COUNT - 1, activeIndex - ret) || index > Math.max(activeIndex + ret, VISIBLE_INDICATORS_COUNT));
const itemCls: string = `indicators-item ${isActive ? 'active' : ''} ${itemInvisible ? 'invisible' : ''} ${this.props.showPreview ? 'preview' : ''}`;
return (
<div
key={index}
className={itemCls}
onClick={this.itemControl.bind(this, index)}>
{this.props.showPreview && (
<div className="image" style={{ background: `url(${item.src})` }}></div>
)}
</div>
)
})
}
private onPrev() {
let index: number = (this.state.activeIndex + this.props.images.length - 1) % this.props.images.length;
this.itemControl(index);
}
private onNext() {
let index: number = (this.state.activeIndex + 1) % this.props.images.length;
this.itemControl(index);
}
private itemControl(index: number) {
if(index === this.state.activeIndex) return;
this.state.activeIndex = index;
this.setState(this.state);
}
private onKeyDown(e: any) {
if(!this.mounted) return;
e.stopPropagation();
switch(e.which || e.keyCode) {
case KEY_CODE.LEFT: this.onPrev();
break;
case KEY_CODE.RIGTH: this.onNext();
break;
}
}
componentDidMount() {
this.mounted = true;
document.documentElement.addEventListener("keydown", this.onKeyDown.bind(this));
}
componentWillUnmount() {
this.mounted = false;
document.documentElement.removeEventListener("keydown", this.onKeyDown.bind(this));
}
render() {
const {
images,
showIndex,
prefixCls
} = this.props;
const { activeIndex } = this.state;
const indicatorVisible: boolean = images.length > 1;
return (
<div className={`react-image-viewer ${prefixCls}-image-viewer`}
ref={(component: any) => this.container = component}>
<ImageWrapper
showIndex={showIndex}
index={`${activeIndex + 1}/${images.length}`}
image={images[activeIndex]}/>
{indicatorVisible ?
<div className="direction-control-button">
<div className="prev-button button"
onClick={this.onPrev.bind(this)}>
<div className="bar"></div>
</div>
<div className="next-button button"
onClick={this.onNext.bind(this)}>
<div className="bar"></div>
</div>
<div className="indicators">
{indicatorVisible && this.renderIndicators(images)}
</div>
</div>
: null}
</div>
)
}
}
interface ModalProps {
images: Image[];
prefixCls?: string;
className?: string;
// Bottom indicators preview
showPreview?: boolean;
// Toolbar index indicator
showIndex?: boolean;
}
class Modal extends Component<ModalProps, any> {
constructor(props: ModalProps) {
super(props);
this.state = {
visible: false,
activeIndex: undefined
}
}
open(activeIndex?: number) {
this.setState({
visible: true,
activeIndex: activeIndex || 0
});
}
close() {
this.setState({
visible: false,
activeIndex: undefined
})
}
render() {
const {
images,
prefixCls,
className,
showIndex,
showPreview
} = this.props;
const { activeIndex } = this.state;
return this.state.visible ? (
<div className='modal'>
<ImageViewer
showPreview={showPreview}
showIndex={showIndex}
prefixCls={prefixCls}
activeIndex={activeIndex}
images={images}/>
<div className='close-button'
onClick={this.close.bind(this)}>
</div>
</div>
) : null;
}
}
class Root extends Component<any, any> {
private modal: Modal;
render() {
const { images } = this.props;
return (
<div className="image-gallery">
{images.map((item: any, index: number) => (
<div className="image-item" key={index}
onClick={this.open.bind(this, index)}>
<div
className="image-inner"
style={{ background: `url(${item.src})`}}>
</div>
</div>
))}
<Modal
images={images}
showIndex
showPreview
ref={(component: any) => this.modal = component}/>
</div>
)
}
open(index: number) {
this.modal.open(index);
}
}
ReactDOM.render(<Root images={data}/>, document.getElementById('root'))
Also see: Tab Triggers