A Pen by Christian Fei on CodePen.
Last active
January 31, 2017 22:22
-
-
Save christian-fei/86504af29d359cd5a2f09fc56ac1ad7b to your computer and use it in GitHub Desktop.
PictureShift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main | |
.picture-shift.fl(data-url="http://lorempixel.com/output/city-q-c-200-200-7.jpg") | |
.picture-shift.fl(data-url="https://s3.amazonaws.com/nautilus-vertical/cosmos-a-128.jpeg") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pictureShifts = document.querySelectorAll('.picture-shift') | |
pictureShifts.forEach(pictureShift => { | |
createDOMMatrixFor(pictureShift.dataset.url) | |
.forEach(tile => pictureShift.appendChild(tile)) | |
intervalFor(pictureShift) | |
}) | |
function intervalFor (pictureShift) { | |
shift(pictureShift) | |
setInterval(() => shift(pictureShift), 120) | |
} | |
function shift (pictureShift) { | |
pictureShift.appendChild(pictureShift.firstChild) | |
} | |
function createDOMMatrixFor(url){ | |
return [1,2,3,4,5,6,7,8,9].map(i => { | |
const cutter = document.createElement('div') | |
cutter.setAttribute('class', `cutter tile-${i}`) | |
cutter.style.backgroundImage = `url(${url})` | |
return cutter | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
width = 200px | |
height = width | |
.picture-shift | |
position relative | |
width width | |
height height | |
border 1px solid lightgrey | |
background-size width height | |
overflow hidden | |
// display inline-block | |
font-size 0 | |
.cutter | |
display inline-block | |
border 0 | |
margin 0 | |
padding 0 | |
width calc(200px/3) | |
height calc(200px/3) | |
background-position 0 0 | |
&.tile-2 | |
background-position calc(-200px/3) 0px | |
&.tile-3 | |
background-position calc(-200px/3 * 2) 0px | |
&.tile-4 | |
background-position 0 calc(-200px/3) | |
&.tile-5 | |
background-position calc(-200px/3) calc(-200px/3) | |
&.tile-6 | |
background-position calc(-200px/3 * 2) calc(-200px/3) | |
&.tile-7 | |
background-position 0 calc(-200px/3 * 2) | |
&.tile-8 | |
background-position calc(-200px/3) calc(-200px/3 * 2) | |
&.tile-9 | |
background-position calc(-200px/3 * 2) calc(-200px/3 * 2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment