Coder Social home page Coder Social logo

beyonk-adventures / svelte-carousel Goto Github PK

View Code? Open in Web Editor NEW
213.0 7.0 39.0 377 KB

A super lightweight, super simple Carousel for Svelte 3

License: MIT License

JavaScript 23.83% Svelte 76.17%
carousel slide javascript svelte svelte-components vanilla-js beyonk siema slider svelte-v3

svelte-carousel's Introduction

This project is now archived. We don't use carousels any more, and you shouldn't either.

Svelte Carousel

js-standard-style CircleCI svelte-v2 svelte-v3

Svelte Carousel / Slider

This is a ground-up rewrite of the original Svelte Carousel/Slider using Svelte v3, and Siema, the goal being a fully working carousel with a tiny size.

Usage

This library is pure javascript, so can be used with any framework you like.

Demo

The simplest possible demo

npm install
npm run dev # http://localhost:12001

To use within a Svelte application:

npm i -D @beyonk/svelte-carousel

Usage

<Carousel>
  <div class="slide-content">Slide 1</div>
  <div class="slide-content">Slide 2</div>
  <div class="slide-content">Slide 3</div>
  <div class="slide-content">Slide 4</div>
</Carousel>

<script>
	import Carousel from '@beyonk/svelte-carousel'
	import { ChevronLeftIcon, ChevronRightIcon } from 'svelte-feather-icons'
</script>

Options

Controls

You can set the controls to be anything you like, though the default height and width are set to 40px. Just use the slots available to insert your own content.

npm i -D svelte-feather-icons
<Carousel>
  <span class="control" slot="left-control">
    <ChevronLeftIcon />
  </span>
  <div class="slide-content">Slide 1</div>
  ...
  <div class="slide-content">Slide n</div>
  <span class="control" slot="right-control">
    <ChevronRightIcon />
  </span>
</Carousel>

<script>
	import Carousel from './Carousel.svelte'
	import { ChevronLeftIcon, ChevronRightIcon } from 'svelte-feather-icons'
</script>

If you need to override height and width, you can use CSS:

	.control :global(svg) {
		width: 100%;
		height: 100%;
		color: #fff;
		border: 2px solid #fff;
		border-radius: 32px;
	}

Attributes

You can pass the following attributes:

Attribute Type Default Value Description
loop boolean true At the end of the carousel, loop through to the first slide again, seamlessly
perPage integer 3 Number of slides on a single page. Note that this needs to be less than or equal to the number of slides in your carousel
autoplay integer 0 Auto-change slide at an interval (in milliseconds). 0 means don't autoplay.
duration number 200 Slide transition duration in milliseconds.
easing string 'ease-out' It is like a CSS transition-timing-function — describes acceleration curve.
startIndex number 0 Index (zero-based) of the starting slide.
draggable boolean true Use dragging and touch swiping.
multipleDrag boolean true Allow dragging to move multiple slides.
dots boolean true Toggles visibility of slider dots.
controls boolean true Toggles visibility of slider controls. dots.
threshold number 20 Touch and mouse dragging threshold (in px).
rtl boolean false Enables layout for languages written from right to left (like Hebrew or Arabic).

perPage can also be set to a responsive object, to change the number of slides based on screen width:

<Carousel perPage={{ 800: 3, 500: 2 }}>
// will show 1 slide below 500px width, 2 at 500, 3 at 800.

Events

The Carousel components emits the following events:

Name Data Description
change { currentSlide, slideCount } currentSlide: The current slide index. Can be a positive or negative integer. slideCount: The number of slides.

Actions

You can call left, right, go(slideNumber), pause and resume functions on the slider. For example, for pausing the autoslide while the mouse is hover the carousel

<Carousel bind:this={carousel} on:mouseenter={enter} on:mouseleave={leave}>
<div class="slide-content">Slide 1</div>
...
<div class="slide-content">Slide n</div>
</Carousel>

<script>
import Carousel from './Carousel.svelte'
let carousel;

function enter() {
  carousel.pause();
}

function leave() {
  carousel.resume();
}
</script>

svelte-carousel's People

Contributors

0xshipthecode avatar antony avatar basaran avatar dependabot[bot] avatar ed-sa-ma avatar eur2 avatar itswadesh avatar jefferyhus avatar jokin avatar mkaski avatar mxmzb avatar snorpey avatar wolfr avatar ximore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

svelte-carousel's Issues

Dots option not working

I cannot deactivate the dots. I've installed the latest version of svelte-carousel:

npm list @beyonk/svelte-carousel
svelte-components
└── @beyonk/[email protected] 

But seems the markup is not up to date to what i can see in this repo. Here you are the component source in node_modules:

<div class="carousel">
	<button class="left" on:click={left}>
		<slot name="left-control"></slot>
	</button>
	<div class="slides" bind:this={siema}>
		<slot></slot>
	</div>
	<ul>
		{#each pips as pip, i}
		<li on:click={() => go(i)} class={currentIndex === i ? "active" : ""}></li>
		{/each}
	</ul>
	<button class="right" on:click={right}>
		<slot name="right-control"></slot>
	</button>
</div>

Can you check it?

(Re)initalize carousel in runtime

I wanted to change the content of the carousel in runtime, but this isn't possible at the moment if I'm right.
I tried to modify the code, but it didn't work.

Is this possible at all without rewriting the code?

Carousel only works when using dev:ssr?

I was really happy to find this, but the joy turned to sadness when I noticed this only works for npm run dev:ssr and not npm run dev. Do I need to add a script or external file to the pages?

Using Routify with Tailwind
node 14.17.0
npm 6.14.13

<script>
	import Carousel from "@beyonk/svelte-carousel";
	import Game from "./game/card.svelte";

	export let data;
	export let type = 1;
</script>

<div class="2xl:pl-0 2xl:pr-0 pl-6 pr-6 relative">
	<Carousel perPage={{ 800: 3, 500: 2 }}>
		{#each data as item}
			<div class="slide-content p-3">
				{#if type == 1}
					<Game {item} />
				{/if}
			</div>
		{/each}
	</Carousel>
</div>

Maybe this is the problem?
Uncaught (in promise) TypeError: Siema is not a constructor

rollup: not found

⚠️ rollup: not found

Hi! I want just how it works.

  1. I clonned this repository
  2. And I send npm run dev

❓ What me did it wrong?

Screenshot from 2019-11-02 20-22-21

fadeIn fadeOut easing option?

Hello, nice slide carousel, i want to implement, but i see that has 'ease-out' option only, How can i set up with fade-in and fade-out or 'fade' mode, is it possible?
many thanks

Invalid Svelte Usage Docs

When using this lib with Svelte as described with the readme, results in Slide component is not defined error.

This is how It worked for me, similar to app/component.svelte

<Carousel current="3">
	<Slide>
		<h2>1</h2>
	</Slide>
	<Slide>
		<h2>2</h2>
	</Slide>
	<Slide>
		<h2>3</h2>
	</Slide>
	<Slide>
		<h2>4</h2>
	</Slide>
	<Slide>
		<h2>5</h2>
	</Slide>
	<Slide>
		<h2>6</h2>
	</Slide>
</Carousel>


<script>
  import Carousel from "node_modules/@beyonk/svelte-carousel/src/components/carousel/component.svelte";
  import Slide from "node_modules/@beyonk/svelte-carousel/src/components/slide/component.svelte";

  export default {
    components: {
      Carousel,
      Slide
    }
  };
</script>

Center primary image

It would be good to have an option that is somewhere between a perpage 3 and perpage 1 where a main image is flanked by two neighbours that are partially of screen.

This could be even more useful if the amount showing could be say 10-5% on mobile and 50-80% on larger screens.

<Carousel> is not a valid SSR component.

When using with Sapper I have this error:
is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

Would be nice to have some instructions how to use it with Sapper

Can Carousel be used inside a link?

Good day,
This is a question, not a bug.
I try to use the carousel component inside anchor element. My goal is to achieve an image carousel like airbnb but swipe actions do not work when placed inside anchor.
Like this:
<a href="/"> <Carousel></Carousel> </a>

Can anyone help me with a suggestion?

Thank you.

Images cannot be updated dynamically

Actually this component and Siema have no idea that images were changed reactively. This causes errors and makes this component unuseful, unfortunately (((

Carousel Crashes randomly on Navigation.

When navigating around my sapper app, I randomly get crashes with the Carousel unmounting.

index.mjs:192 Uncaught (in promise) TypeError: Cannot read property 'removeChild' of null
    at detach (index.mjs:192)
    at detach_dev (index.mjs:1557)
    at Object.destroy [as d] (Slider.svelte:29)
    at Object.destroy [as d] (Carousel.svelte:14)
    at destroy_component (index.mjs:1407)
    at Object.destroy [as d] (Slider.svelte:12)
    at destroy_component (index.mjs:1407)
    at Object.destroy [as d] (index.svelte:17)
    at destroy_component (index.mjs:1407)
    at App.svelte:24

Control the slider from the parent component

I want to control the slides (move left, right, select an specific slide, pause the autoslider when the carousel is focused or hovered).

I make it work exporting the left - right functions, is there another way without having to export the functions?

If do you think is something useful, I can make a pull request adding some comments on the usage to the readme.

Not vanilla-js

Hi! On your site you have a vanilla-js tag. However, this project is focused on Svelte, a JS Framework. In order to make sure your project is correctly described, could you remove that tag?

<Carousel> is not a valid SSR component when used in SvelteKit

I am migrating my project from sapper to SvelteKit but I am getting:

Error: <Carousel> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

the import is happening in <script> tag:

import Carousel from '@beyonk/svelte-carousel';
import { ChevronLeftIcon, ChevronRightIcon } from 'svelte-feather-icons';

Make a codesandbox example

This would be much better with a link to a codesandbox so we can quickly see it and decide to use it or now. Definitely boosts star count.

Add license information

Hi there,

thank you for creating this library. I would like to use it in one of my projects, but could not find any kind of license information in the repository. Could you add some kind of license information?

Thank you!

Add on:ready event or similar?

I'm using the AOS library here: http://michalsnik.github.io/aos/
It's great and generally works well with Svelte. But mixing it with a carousel is giving me some issues, such as AOS's offset calculations being out. What I'd like to do is only initialise AOS when the carousel has built. I'm trying:
$: if (carousel) AOS.init()
But that doesn't help. Could we have an on:ready event or something like that that I can listen for before initialising AOS? I'm sure it would help others.

Request: Disable prev/next controls

Nice work on this!

Could we include an option for disabling controls similar to the dots? I've simply omitted them and they're causing errors in a lighthouse audit as the buttons do not have a discernable name.

perPage working but with error message

The perPage function used like "perPage={{ 800: 3, 500: 2 }}" generates an error message "Invalid array length" and even though it works as expected it fucks up other functions on the site.
Skärmavbild 2020-06-25 kl  09 57 49

Constant spinning Slider when autoplay is on, even when duration is set.

Can't seem to stop the slider from spinning around like crazy when autoplay is on.

<script>
	import Carousel from '@beyonk/svelte-carousel'
  import { ChevronLeftIcon, ChevronRightIcon } from 'svelte-feather-icons'
  
  import Banner_1 from '../images/Banners_lunch.jpg'
  import Banner_2 from '../images/Banners_craft.jpg';
  import Banner_3 from '../images/Banners_restaurant.jpg';
  import Banner_4 from '../images/Banners_entertainment.jpg';
  import Banner_5 from '../images/Banners_functions.jpg';
</script>

<Carousel perPage="1" autoplay duration={2000}>
  <span class="control" slot="left-control">
    <ChevronLeftIcon />
  </span>
  <banner class="slide-content">
    <img src={Banner_1} alt={Banner_1} />
  </banner>
  <banner class="slide-content">
    <img src={Banner_2} alt={Banner_2} />
  </banner>
  <banner class="slide-content">
    <img src={Banner_3} alt={Banner_3} />
  </banner>
  <banner class="slide-content">
    <img src={Banner_4} alt={Banner_4} />
  </banner>
  <banner class="slide-content">
    <img src={Banner_5} alt={Banner_5} />
  </banner>
  <span class="control" slot="right-control">
    <ChevronRightIcon />
  </span>
</Carousel>

<style>
  banner {
    width: 100%;
    height: auto;
  }
  span {
    display: inline-block; 
    width: 60px;
    height: 60px;
    border-radius: 30px;
    background: rgba(0, 0, 0, 0.4);
    color: white;
  }
  img {
    width: 100%;
    height: auto;
  }
</style>

Not possible to add slides dynamically

Currently is not possible to add slides dynamically to the Carousel.

eg:

<Carousel>
   {#each images as image}
        <div>image</div>
   {/each}
</Carousel>

If you add more items to the array, you will get a svelte error due to the usage of .

Suggestion:
Use the selector from Siema. That way we can do something:

<Carousel selector=".slides-here">
   <div class="slides-here">
   {#each images as image}
        <div>image</div>
   {/each}
   </div>
</Carousel>

How to change the layout and styles of Carousel Items on mobile

Currently each item in the Carousel span 30% width and is floated to left.

I'm trying to figure out a way via which I can make the carousel items span 100% or make the width auto and remove the float. I did the following to acheive the above but it breaks the carousel on mobile and while dragging to left and right some items at the end are skipped with a glitch.

.slides {
  & > div {
    display: flex;
    & > * {
      float: none !important;
      width: auto !important;

      &:not(:last-child) {
        margin-right: var(--xx-small);
      }
    }
  }
}

It would be nice If one can have full control of styling and layout while keeping the functionalty intact across different devices. Thanks

Getting error while importing

Module not found: Error: Can't resolve './Carousel.svelte'
Module not found: Error: Can't resolve 'svelte-feather-icons'

I have followed the steps exactly how its described in "Usage" section.

npm install  @beyonk/svelte-carousel

<Carousel>
  <span class="control" slot="left-control">
    <ChevronLeftIcon />
  </span>
  <div class="slide-content">Slide 1</div>
  <div class="slide-content">Slide 2</div>
  <div class="slide-content">Slide 3</div>
  <div class="slide-content">Slide 4</div>
  <span class="control" slot="right-control">
    <ChevronRightIcon />
  </span>
</Carousel>

<script>
	import Carousel from './Carousel.svelte'
	import { ChevronLeftIcon, ChevronRightIcon } from 'svelte-feather-icons'
</script>

Add change dispatcher with current index

The carousel works great, one feature I would love to have is an on:change handler to return the current slide index. I would PR this but I can't get my fork working locally..

Siema already has an onChange event in the controller, so it might only be a matter of dispatching this

Custom Colors

Would be great to be able to customize default control colors.

For example:

export default {
	data() {
		carouselControlsButtonColor: "rgba(255, 255, 255, 0)";
	}
	components: {
		Carousel,
		Slide
	}
};

And Carousel would inherit those properies.

Uncaught (in promise) TypeError: Cannot read property 'removeChild' of null

I love this module but I got an error when use #each on slide content on Sapper v0.27.4

Uncaught (in promise) TypeError: Cannot read property 'removeChild' of null

btw I have found similar issue on svelte repo sveltejs/svelte#2086, maybe upgrading svelte version in this module can fix the issue

My code :

<script>
  import Carousel from "@beyonk/svelte-carousel";
  import SingleCard from "./SingleCard.svelte";
  export let windowWidth;

  const singleMaterials = {
    title: "Just a title",
    author: "authorx",
    description: "card description"  };
  const materials = [
    singleMaterials,
    singleMaterials,
    singleMaterials,
    singleMaterials,
    singleMaterials,
    singleMaterials
  ];
</script>

<Carousel loop={false} perPage={windowWidth}>
  {#each materials as material}
    <SingleCard {material} />
  {/each}
</Carousel>

Pause autoplay on hover

Would be cool to pause the autoplay on hover, and even cooler to reset the autoplay countdown after the user has paginated manually (e.g. autoplay={5000}, and after 2500ms the user clicks the next slide. Instead of going to the next slide after the left 2500ms, it should start counting from 0 again).

Thanks, and best regards!

Iterate through an array

Hi!

I have an array that I would like to add to the carousel, the goal is that depending on the number of objects on the array the carousel will be automatically mounted with those objects. The problem is that when using #each the results of the array are all mounted on the first item of the carousel

Code:

<div class="demo">
  {#each carousels as carousel}
    <Carousel on:change={changed} {...carousel}>
      <span class="control" slot="left-control">
        <ChevronLeftIcon />
      </span>
      {#await portsBoiksResult then items}
        {#each items as item}
          <div class="mainDivStyle">
            <div class="boxStyle">
              <p>{item.title}</p>
            </div>
          </div>
        {/each}
      {:catch error}
        <p style="color: red">{error.message}</p>
      {/await}
      <span class="control" slot="right-control">
        <ChevronRightIcon />
      </span>
    </Carousel>
    <br />
    <br />
  {/each}
</div>

Thanks!

Duplicate loop attributes

Hey, this carousel is really helpful, thanks. It seems that there are 2 descriptions of the loop attribute in the docs. The one that seems to work is the one that controls the autoplay loop. Currently I'm having trouble stopping the carousel from looping with autoplay set to false.

Dots is not working properly when there is more than 1 item per page

Dots should work similarly as pagination. eg:

If we have 6 items, and we set perPage={2}, it should appear only 3 dots. However, is appearing 6 dots (one for each item), and this is causing a bug, as we can even see in the demo link in the READ.me:

https://svelte.dev/repl/3953567d530b41d087ab7eaa8e7e632a?version=3.22.3

We can see we have 4 items and 4 dots. but we should have only 2 dots.
4 / perPage(3 default) = Math.ceil(1.3) = 2.

We also can see that after dragging to right, the dots will remain without the class "active" until it gets to the item 0 again

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.