Vue Gallery - Switch Between Images

User Interaction

To switch between images on touch-enabled devices, the user can perform the swipe gesture. A more desktop-oriented solution for the same purpose are the Next and Previous navigation buttons. You can control the swipe gesture and the navigation buttons using the swipeEnabled and showNavButtons properties, respectively.

  • <template>
  • <DxGallery
  • :data-source="dataSource"
  • :height="300"
  • :swipe-enabled="false"
  • :show-nav-buttons="true"
  • />
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxGallery } from 'devextreme-vue/gallery';
  •  
  • export default {
  • components: {
  • DxGallery
  • },
  • data() {
  • return {
  • dataSource: [
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person1.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person2.png'
  • ]
  • };
  • }
  • }
  • </script>

With the buttons and swipe gesture, the user switches images in a particular order, and once the last image is reached, the user can only switch back. For this case, you can enable the user to jump straight to the first image, if you set the loop property to true.

  • <template>
  • <DxGallery
  • :data-source="dataSource"
  • :height="300"
  • :loop="true"
  • />
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxGallery } from 'devextreme-vue/gallery';
  •  
  • export default {
  • components: {
  • DxGallery
  • },
  • data() {
  • return {
  • dataSource: [
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person1.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person2.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person3.png'
  • ]
  • };
  • }
  • }
  • </script>

Below the current image, the Gallery shows navigation bullets that allow the user to switch images ignoring their order. To disable the navigation bullets, set the indicatorEnabled property to false. If you need to hide them completely, assign false to the showIndicator property.

  • <template>
  • <DxGallery
  • :data-source="dataSource"
  • :height="300"
  • :indicator-enabled="false"
  • />
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxGallery } from 'devextreme-vue/gallery';
  •  
  • export default {
  • components: {
  • DxGallery
  • },
  • data() {
  • return {
  • dataSource: [
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person1.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person2.png'
  • ]
  • };
  • }
  • }
  • </script>
See Also

API

NOTE
In this article, the Button UI component is used to switch images. This choice is made for purely demonstrational purposes, and you can do the same operations using another UI component following the same guidelines.

To switch the Gallery to the next or previous image, call the nextItem(animation) or prevItem(animation) method, respectively.

  • <template>
  • <div>
  • <DxGallery
  • :data-source="dataSource"
  • :height="300"
  • ref="gallery"
  • />
  • <DxButton
  • text="Next"
  • @click="goToNextItem"
  • />
  • <DxButton
  • text="Previous"
  • @click="goToPreviousItem"
  • />
  • </div>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxGallery } from 'devextreme-vue/gallery';
  • import { DxButton } from 'devextreme-vue/button';
  •  
  • export default {
  • components: {
  • DxGallery,
  • DxButton
  • },
  • data() {
  • return {
  • dataSource: [
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person1.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person2.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person3.png',
  • 'https://js.devexpress.com/Content/images/doc/24_2/PhoneJS/person4.png'
  • ]
  • };
  • },
  • methods: {
  • goToNextItem () {
  • this.$refs.gallery.instance.nextItem(true);
  • }
  • goToPreviousItem () {
  • this.$refs.gallery.instance.prevItem(true);
  • }
  • }
  • }
  • </script>

To navigate to a specific image, call the goToItem(itemIndex, animation) method. The first argument should be the index of the required image in the dataSource.

  • <template>
  • <DxGallery
  • ref="gallery"
  • />
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { DxGallery } from 'devextreme-vue/gallery';
  •  
  • export default {
  • components: {
  • DxGallery
  • },
  • methods: {
  • goToItem (index) {
  • this.$refs.gallery.instance.goToItem(index, true);
  • }
  • }
  • }
  • </script>
See Also