JavaScript/jQuery 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.

JavaScript
  • $(function () {
  • $("#galleryContainer").dxGallery({
  • 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"
  • ],
  • height: 300,
  • swipeEnabled: false,
  • showNavButtons: true
  • });
  • });

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.

JavaScript
  • $(function () {
  • $("#galleryContainer").dxGallery({
  • 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"
  • ],
  • height: 300,
  • loop: true
  • });
  • });

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.

JavaScript
  • $(function () {
  • $("#galleryContainer").dxGallery({
  • 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"
  • ],
  • height: 300,
  • indicatorEnabled: false
  • });
  • });
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.

JavaScript
  • $(function () {
  • const gallery = $("#galleryContainer").dxGallery({
  • 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"
  • ],
  • height: 300
  • }).dxGallery("instance");
  •  
  • $("#nextButton").dxButton({
  • text: "Next",
  • onClick: function () {
  • gallery.nextItem(true);
  • }
  • });
  •  
  • $("#prevButton").dxButton({
  • text: "Previous",
  • onClick: function () {
  • gallery.prevItem(true);
  • }
  • });
  • });

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.

JavaScript
  • const gallery = $("#galleryContainer").dxGallery("instance");
  • // Goes to the third image
  • gallery.goToItem(2, true);
See Also