Skip to content

Instantly share code, notes, and snippets.

@rye761
Created July 25, 2017 17:06
Show Gist options
  • Save rye761/eb3afcb328e4e85223f51f43aec052ea to your computer and use it in GitHub Desktop.
Save rye761/eb3afcb328e4e85223f51f43aec052ea to your computer and use it in GitHub Desktop.
Simple Vue.js Slideshow
<html>
<head>
<title>Slideshow Test</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="container">
<i @click="moveLeft" class="fa fa-2x fa-fw fa-chevron-left"></i>
<div id="slideshow">
<div id="slideshow-items" :style="{ transform: transformStyle }">
<div v-for="slide in slides" :class="slide.class"></div>
</div>
</div>
<i @click="moveRight" class="fa fa-fw fa-2x fa-chevron-right"></i>
</div>
<span>This is some text</span>
<script>
var vm = new Vue({
el: '#container',
data: {
item: 0,
slides: [
{ 'class': 'circle' },
{ 'class': 'square' },
{ 'class': 'triangle' }
],
timer: null
},
computed: {
transformStyle: function() {
// We need to advance the slideshow 200px for every item.
var result = 'translateX(' + (-200 *this.item) + 'px)';
return result;
}
},
methods: {
moveLeft: function() {
if (this.item > 0) {
this.item--;
}
},
moveRight: function() {
if (this.item >= (this.slides.length - 1)) {
this.item = 0;
return;
}
this.item++;
}
},
mounted: function() {
this.timer = setInterval(this.moveRight, 3000);
}
});
</script>
</body>
</html>
#slideshow {
width: 200px;
height: 200px;
font-size: 0;
overflow: hidden;
display: inline-block;
vertical-align: middle;
}
#slideshow-items {
white-space: nowrap;
transition: transform .5s ease;
}
i {
cursor: pointer;
}
.square {
width: 200px;
height: 200px;
background-color: tomato;
display: inline-block;
}
.triangle {
width: 0;
height: 0;
border-bottom: 173px solid cornflowerblue;
border-right: 100px solid transparent;
border-left: 100px solid transparent;
display: inline-block;
}
.circle {
width: 200px;
height: 200px;
background-color: limegreen;
border-radius: 50%;
display: inline-block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment