Categories
Vue 3 Projects

Create an Image Slider App with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create an image slider with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create image-slider

and select all the default options to create the project.

Create the Image Slider

To create the image slider, we write:

<template>
  <div>
    <button @click="prev">&lt;</button>
    <img :src="image" />
    <button @click="next">&gt;</button>
  </div>
</template>

<script>
const images = [
  "https://i.picsum.photos/id/1/200/300.jpg?hmac=jH5bDkLr6Tgy3oAg5khKCHeunZMHq0ehBZr6vGifPLY",
  "https://i.picsum.photos/id/2/200/300.jpg?hmac=HiDjvfge5yCzj935PIMj1qOf4KtvrfqWX3j4z1huDaU",
  "https://i.picsum.photos/id/3/200/300.jpg?hmac=o1-38H2y96Nm7qbRf8Aua54lF97OFQSHR41ATNErqFc",
];
export default {
  name: "App",
  data() {
    return { index: 0, image: images[0] };
  },
  methods: {
    next() {
      this.index = (this.index + 1) % images.length;
      this.image = images[this.index];
    },
    prev() {
      this.index = (this.index - 1) % images.length;
      this.image = images[this.index];
    },
  },
};
</script>

We create the button with &lt; to show the less than symbol.

When we click it, it runs the prev method to move to the previous image.

The previous image is set by subtracting this.index by 1 and get the remainder of that when divided by images.length .

Likewise, we have the button with &gt; to show the great than symbol.

When we click it, it runs the next method to move to the previous image.

The previous image is set by add 1 to this.index and get the remainder of that when divided by images.length .

To display the image, we have the img element with the src prop set to image , which has the URL of the selected image.

In the data method, we have image is set to images[0] to display the first image initially.

And we have the index set to 0 to keep the index in sync with image .

To make a slider that changes the image automatically, we write:

<template>
  <div>
    <img :src="image" />
  </div>
</template>

<script>
const images = [
  "https://i.picsum.photos/id/1/200/300.jpg?hmac=jH5bDkLr6Tgy3oAg5khKCHeunZMHq0ehBZr6vGifPLY",
  "https://i.picsum.photos/id/2/200/300.jpg?hmac=HiDjvfge5yCzj935PIMj1qOf4KtvrfqWX3j4z1huDaU",
  "https://i.picsum.photos/id/3/200/300.jpg?hmac=o1-38H2y96Nm7qbRf8Aua54lF97OFQSHR41ATNErqFc",
];
export default {
  name: "App",
  data() {
    return { index: 0, image: images[0] };
  },
  methods: {
    next() {
      this.index = (this.index + 1) % images.length;
      this.image = images[this.index];
    },
    autoChangeSlide() {
      setInterval(() => {
        this.next();
      }, 1000);
    },
  },
  beforeMount() {
    this.autoChangeSlide();
  },
};
</script>

We call setInterval with a callback that calls this.next to move to the next slide every second as specified in the 2nd argument of setInterval .

Conclusion

We can create image slider app easily with Vue 3.

Categories
Vue 3 Projects

Create a Counter App with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a counter with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create counter

and select all the default options to create the project.

Create the Counter

We can create the counter by writing:

<template>
  <div>
    <button @click="count++">increment</button>
    <button @click="count--">decrement</button>
    <p>{{ count }}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return { count: 0 };
  },
};
</script>

We have the increment button that increments the count reactive property by 1 when we click on it.

And we have the decrement button that decrements the count reactive property by 1 when we click on it.

In the component object, we have the data method that returns an object with count set to 0 as its initial value.

This defines the count reactive property so we can use it in our template.

We can also access it as this.count in the component object.

Also, we can write:

<template>
  <div>
    <button @click="increment">increment</button>
    <button @click="decrement">decrement</button>
    <p>{{ count }}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return { count: 0 };
  },
  methods: {
    increment() {
      this.count++;
    },
    decrement() {
      this.count--;
    },
  },
};
</script>

to move count++ to increment and count-- to decrement .

We access count as a property of this in the component to access the reactive property.

Conclusion

We can create a counter app easily with Vue 3.

Categories
Vue 3 Projects

Create a Random Quote Text Generator with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a random text generator with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create quote-text-generator

and select all the default options to create the project.

Create the Quote Text Generator

Now that we have the project created, we can go into the App.vue file and write:

<template>
  <div>
    <button @click="generate">generate</button>
    <p>{{ quote.quote }}</p>
    <p>{{ quote.cite }}</p>
  </div>
</template>

<script>
const quotes = [
  {
    quote:
      "One of my most productive days was throwing away 1,000 lines of code.",
    cite: "Ken Thompson",
  },
  {
    quote:
      "I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.",
    cite: "Bjarne Stroustrup",
  },
  {
    quote: "When in doubt, use brute force.",
    cite: "Ken Thompson",
  },
  {
    quote: "Talk is cheap. Show me the code.",
    cite: "Linus Torvalds",
  },
  {
    quote:
      "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.",
    cite: "Martin Golding",
  },
  {
    quote:
      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.",
    cite: "Linus Torvalds",
  },
  {
    quote:
      "Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves.",
    cite: "Alan Kay",
  },
  {
    quote:
      "Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris",
    cite: "Larry Wall",
  },
  {
    quote:
      "First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack.",
    cite: "George Carrette",
  },
];

export default {
  name: "App",
  data() {
    return { quote: {} };
  },
  methods: {
    generate() {
      const index = Math.floor(Math.random() * quotes.length);
      this.quote = quotes[index];
    },
  },
};
</script>

We have the generate button in the template to get the quotes from the quotes array.

Then we display the quote and cite properties from the selected entry.

In the template, we have the generate method to select the quote.

We do this by calling Math.floor to round down to the nearest integer.

We get a random number between 0 and quotes.length by writing Math.random() * quotes.length .

Then quotes[index] gets the choice and we assign that to this.quote so we can display the choice.

We also have to define this.quote in the data function by returning an object with the quote property.

Conclusion

We can create a random quote text generate easily with Vue 3.

Categories
Vue 3

Add a Swiper Carousel into a Vue 3 App with Swiper 6

Swiper for Vue.js lets us add a carousel to our Vue 3 app.

In this article, we’ll look at how to add a carousel into our Vue 3 app with Swiper.

Thumbnail Swiper

We can add a thumbnail swiper into our Vue 3 app with Swiper 6.

For instance, we can write:

<template>
  <swiper :thumbs="{ swiper: thumbsSwiper }">
    <swiper-slide v-for="n of 50" :key="n"
      >Slide {{ n }}</swiper-slide
    >
  </swiper>

  <swiper
    @swiper="setThumbsSwiper"
    watch-slides-visibility
    watch-slides-progress
  >
    <swiper-slide v-for="n of 50" :virtualIndex="n" :key="n"
      >Thumbnail {{ n }}</swiper-slide
    >
  </swiper>
</template>

<script>
import SwiperCore, { Thumbs } from "swiper";
import { Swiper, SwiperSlide } from "swiper/vue";
import "swiper/swiper-bundle.css";

SwiperCore.use([Thumbs]);

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      thumbsSwiper: null,
    };
  },
  methods: {
    setThumbsSwiper(swiper) {
      this.thumbsSwiper = swiper;
    },
  },
};
</script>

to make the 2nd swiper the thumbnail swiper.

We add the watch-slides-visibility and watch-slides-progress prop to watch the progress of the first slider.

The first swiper is watched because the setThumbsSwiper method is called.

And we set the this.thumbSwiper reactive property to the 2nd swiper.

Effects

Swiper 6 comes with the following effects:

  • Fade
  • Cube
  • Overflow
  • Flip

We can write:

<template>
  <swiper effect="fade">
    <swiper-slide v-for="i of images" :key="i">
      <img :src="i" />
    </swiper-slide>
  </swiper>
</template>

<script>
import SwiperCore, { EffectFade } from "swiper";
import { Swiper, SwiperSlide } from "swiper/vue";
import "swiper/swiper.scss";
import "swiper/components/effect-fade/effect-fade.scss";

SwiperCore.use([EffectFade]);

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      images: [
        "https://i.picsum.photos/id/23/200/300.jpg?hmac=NFze_vylqSEkX21kuRKSe8pp6Em-4ETfOE-oyLVCvJo",
        "https://i.picsum.photos/id/25/200/300.jpg?hmac=ScdLbPfGd_kI3MUHvJUb12Fsg1meDQEaHY_mM613BVM",
        "https://i.picsum.photos/id/26/200/300.jpg?hmac=E9i_aIqa_ifLvxqI2b1QTLCnhGQYJ83IpvaDfFM54bU",
      ],
    };
  },
};
</script>

to add the effect prop to the swiper component.

We also have to add the SCSS files for the effect animation and SwiperCore.use([EffectFade]) to add the fade effect.

We can add the other effects in similar ways, except we replace the SCSS and the module we call use with.

Conclusion

We can add thumbnail swipers and swiper effects into our Vue 3 app with Swiper 6.

Categories
Vue 3

Add a Swiper Carousel into a Vue 3 App with Swiper 6

Swiper for Vue.js lets us add a carousel to our Vue 3 app.

In this article, we’ll look at how to add a carousel into our Vue 3 app with Swiper.

Installation

We can install the Swiper package by running:

npm i swiper

Basic Usage

Once we installed the package, we can add a slider by writing:

<template>
  <swiper
    :slides-per-view="3"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="n of 50" :key="n">Slide {{ n }}</swiper-slide>
  </swiper>
</template>
<script>
import { Swiper, SwiperSlide } from "swiper/vue";
import 'swiper/swiper-bundle.css'

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  methods: {
    onSwiper(swiper) {
      console.log(swiper);
    },
    onSlideChange() {
      console.log("slide change");
    },
  },
};
</script>

We add the swiper component to add the slider.

slides-per-view lets us set the number of slides to show per view.

space-between has the space between slides in pixels.

It emits the swiper event which is emitted when the slider is created.

slideChange event is emitted when we change slides.

We import the swiper/swiper-bundle.css file to add styles for the slides.

swiper-slide has the slides for the slider.

We can add the zoom prop to enable additional wrapper for zoom mode if it’s set to true .

virtualIndex has the index for virtual slides.

SwiperSlide Slot Props

We can get some data from the swiper-slide ‘s component’s slot props.

For example, we get the isActive prop to see if the slide is active:

<template>
  <swiper
    :slides-per-view="3"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-slot="{ isActive }" v-for="n of 50" :key="n"
      >Slide {{ n }} {{ isActive ? "active" : "not active" }}</swiper-slide
    >
  </swiper>
</template>
<script>
import { Swiper, SwiperSlide } from "swiper/vue";
import "swiper/swiper-bundle.css";

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  methods: {
    onSwiper(swiper) {
      console.log(swiper);
    },
    onSlideChange() {
      console.log("slide change");
    },
  },
};
</script>

It also provides the isPrev slot prop to see whether a slide is a slide previous to the active one.

The isNext slot prop to see whether a slide is a slide next to the active one.

isVisible indicates whether the current slide is visible.

isDuplicate indicates whether the slide is a duplicate slide.

Slots

We can populate slots provided by the swiper component.

For instance, we can write:

<template>
  <swiper
    :slides-per-view="3"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="n of 50" :key="n">Slide {{ n }}</swiper-slide>
    <template v-slot:container-start>
      <span>Container Start</span>
    </template>
    <template v-slot:container-end>
      <span>Container End</span>
    </template>
    <template v-slot:wrapper-start>
      <span>Wrapper Start</span>
    </template>
    <template v-slot:wrapper-end>
      <span>Wrapper End</span>
    </template>
  </swiper>
</template>
<script>
import { Swiper, SwiperSlide } from "swiper/vue";
import "swiper/swiper-bundle.css";

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  methods: {
    onSwiper(swiper) {
      console.log(swiper);
    },
    onSlideChange() {
      console.log("slide change");
    },
  },
};
</script>

We populate the container-start slot to add content to the top of the slider container.

container-end adds content to the bottom of the slider container

wrapper-start adds content before the first slide.

wrapper-end adds content after the last slide.

Conclsion

We can add sliders into our Vue 3 app with Swiper 6.