GithubHelp home page GithubHelp logo

marcojakob / dart-swiper Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 3.0 1.08 MB

A touch (and mouse) slider for swiping through images and html.

License: MIT License

CSS 13.03% Dart 43.72% HTML 41.61% Shell 1.64%

dart-swiper's Introduction

Swiper

A touch (and mouse) slider for swiping through images and HTML.

Star this Repo Pub Package Build Status

GitHub | Pub | Demos and Examples

Browser Support

Swiper supports all browsers supported by Dart 1.6 and later:

  • Internet Explorer, versions 10, and 11.
  • Firefox, latest version.
  • Chrome, latest version.
  • Safari for desktop, version 6.
  • Safari for mobile, version 6.

Features

  • Touch and Mouse. Slide either on a touch screen or with the mouse.
  • Smooth Transitions. Swiper uses Hardware-accelerated CSS3 transitions for smooth animations.
  • Auto Resizing. When the browser window is resized or a mobile device is rotated, the swiper and all its pages are resized automatically.
  • Scroll Prevention. Swiper detects if the user tries to slide or tries to scroll vertically.
  • Images or HTML. Swiper supports any HTML content for swipe pages.

Demos

All Demos can also be found in the example folder.

Usage

1. HTML

Swiper needs a simple HTML structure. Here is an example:

<div class="swiper">
  <div class="page-container">
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>
  • The swiper is the main container. This will become the viewport.
  • The page-container is the container that wraps all pages.
  • The inner divs are the slide pages and can contain any HTML content.

2. Initialize the Swiper

In the Dart code you initialize the Swiper with a single line. The main container needs to be passed to the Swiper constructor.

Swiper swiper = new Swiper(querySelector('.swiper'));

3. CSS

A few styles are needed:

.swiper {
  overflow: hidden;
  position: relative;
  height: 333px; /* Declare the height of the swiper. */
  visibility: hidden; /* Hide until layout is ready. */
}

.page-container {
  position: relative;
  height: 100%;
}

.page-container > div {
  position: absolute;
  width: 100%;
}

Constructor Options

  • startIndex: the index position the Swiper should start at - default: 0
  • speed: the speed of prev and next transitions in milliseconds - default: 300
  • autoWidth: defines if the Swiper should automatically adjust the width when the browser is resized. You might want to disable this if you want to manually do some width and height calculations. If disabled, make sure you call the resize method after the size of the Swiper changed - default: true
  • autoHeightRatio defines if and how the Swiper should calculate the height. If defined, the height is calculated from the Swiper's width with autoHeightRatio and automatically applied when the browser is resized. This is useful, e.g. for responsive images - default: null
  • distanceThreshold: If swipe distance is more than the distanceTreshold (in px), a swipe is detected (regardless of swipe duration) - default: 20
  • durationThreshold: If swipe duration is less than the durationTreshold (in ms), a swipe is detected (regardless of swipe distance) - default: 250
  • handle: If handle query String is specified, it restricts the dragging from starting unless it occurs on the specified element(s). Only elements that descend from the swiperElement are permitted - default: null
  • cancel: If cancel query String is specified, drag starting is prevented on specified elements - default: input, textarea, button, select, option
  • draggingClass: Is the CSS class set to the swiperElement while a user is dragging. If set to null, no such CSS class is added - default: swiper-dragging
  • draggingClassBody: Is the CSS class set to the html body tag while a user is dragging. If set to null, no such CSS class is added - default: swiper-drag-occurring

Example

Swiper swiper = new Swiper(querySelector('.swiper'), 
    startIndex: 2,
    speed: 600,
    autoWidth: true,
    autoHeightRatio: 0.66);

Methods

  • currentIndex: The current page index.
  • currentPage: The HTML element of the current page.
  • moveToIndex(int index, {int speed, bool noEvents: false}: Moves to the page at the specified index. Optionally, the speed of the transition can be specified. If no events (pageChange and pageTransitionEnd) should be fired, the noEvents flag can be set to true.
  • next({int speed}): Moves to the next page.
  • prev({int speed}): Moves to the previous page.
  • hasNext(): Returns true if there is a next page.
  • hasPrev(): Returns true if there is a previous page.
  • resize(): Updates the cached page width and the container sizes. The resize method is automatically called when the browser is resized. But if the Swiper is resized other than through browser resizing, the resize method must be called manually.
  • destroy(): Unistalls all listeners. This will return the swiper element back to its pre-init state.

Example

// Set up method calls for button clicks.
ButtonElement prevButton = querySelector('#previous-button')
    ..onClick.listen((_) => swiper.prev());
ButtonElement nextButton = querySelector('#next-button')
    ..onClick.listen((_) => swiper.next());

Events

  • onPageChange: Fired when the current page changed. The data of the event is the page index.
  • onPageTransitionEnd: Fired when the transition ends after a page change. Note: If the user swipes again before the previous transition ended, this event is only fired once, at the end of all page transitions. The data of the event is the page index.
  • onDragStart: Fired when the user starts dragging.
  • onDrag: Fired periodically throughout the drag operation.
  • onDragEnd: Fired when the user ends the dragging.

Example

swiper.onPageChange.listen((index) {
  print('PageChange Event: index=${index}');
});

Attribution

Swiper is heavily inspiried by flipsnap.js, swipe.js, and SwipeView.

Many thanks to the authors for those great JavaScript projects!

License

The MIT License (MIT)

dart-swiper's People

Contributors

marcojakob avatar mrwade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.