Giter Site home page Giter Site logo

lunarlogic / angular-bootstrap-lightbox Goto Github PK

View Code? Open in Web Editor NEW

This project forked from compact/angular-bootstrap-lightbox

1.0 10.0 0.0 361 KB

An AngularJS lightbox built using UI Bootstrap Modal.

Home Page: http://compact.github.io/angular-bootstrap-lightbox/

License: MIT License

JavaScript 85.45% CSS 9.19% HTML 5.37%

angular-bootstrap-lightbox's Introduction

angular-bootstrap-lightbox

This lightbox displays images using an AngularUI Bootstrap Modal.

When the lightbox is opened, navigating to the previous/next image can be achieved by clicking buttons above the image, clicking the left/right arrow keys, or swiping to the left/right (optional with ngTouch). The escape key for closing the modal is automatically binded by AngularUI Bootstrap.

Large images are scaled to fit inside the window. An optional image caption overlays the top left corner of the image.

Demos

Demos

API documentation

API documentation of the services and directive in the Angular module

Setup

  1. Install in one of the following ways:
  • Install with Bower: bower install angular-bootstrap-lightbox --save
  • Install with npm: npm install angular-bootstrap-lightbox --save
  • Manually save the script and stylesheet from dist.
  1. Include the stylesheet in your app:
<link rel="stylesheet" href="bower_components/angular-bootstrap-lightbox/dist/angular-bootstrap-lightbox.css">
  1. Include the script in your app:
<script src="bower_components/angular-bootstrap-lightbox/dist/angular-bootstrap-lightbox.js"></script>
  1. The Angular module is named bootstrapLightbox. Add it as a dependency to your module:
angular.module('app', ['bootstrapLightbox']);
  1. Optional dependencies:
  • To enable swipe navigation in the lightbox, include the ngTouch script before angular-bootstrap-lightbox.js.
  • To show a loading bar while an image is loading, include the angular-loading-bar script before angular-bootstrap-lightbox.js.

Basic example

Gallery:

<ul ng-controller="GalleryCtrl">
  <li ng-repeat="image in images">
    <a ng-click="openLightboxModal($index)">
      <img ng-src="{{image.thumbUrl}}" class="img-thumbnail">
    </a>
  </li>
</ul>

Controller:

angular.module('app').controller('GalleryCtrl', function ($scope, Lightbox) {
  $scope.images = [
    {
      'url': '1.jpg',
      'caption': 'Optional caption',
      'thumbUrl': 'thumb1.jpg' // used only for this example
    },
    {
      'url': '2.gif',
      'thumbUrl': 'thumb2.jpg'
    },
    {
      'url': '3.png',
      'thumbUrl': 'thumb3.png'
    }
  ];

  $scope.openLightboxModal = function (index) {
    Lightbox.openModal($scope.images, index);
  };
});

Configuration

Changing the appearance of the modal lightbox

The default view template for the lightbox is lightbox.html. Its look can be changed by making your own custom template and/or adding CSS rules; for example, use the selector .lightbox-image-caption to style the caption.

If you make your own template and save it at lightbox.html, no further code is necessary. If you save it at a different path, set it in the provider:

angular.module('app').config(function (LightboxProvider) {
  // set a custom template
  LightboxProvider.templateUrl = 'path/to/your-template.html';
});

Disabling the keyboard navigation

The keyboard navigation in the lightbox with the left/right arrow keys can be enabled/disabled at any time by changing the value of the boolean Lightbox.keyboardNavEnabled.

Array of images

The first argument to Lightbox.openModal must be an array, and its elements may be of any type. In the basic example above, it is an array of objects with properties url and caption, but this is only the default and is not required. To let the Lightbox service know the correct values, set these methods in the provider:

angular.module('app').config(function (LightboxProvider) {
  LightboxProvider.getImageUrl = function (image) {
    return '/base/dir/' + image.getName();
  };

  LightboxProvider.getImageCaption = function (image) {
    return image.label;
  };
});

For a more specific example, if you have no captions, the array can contain strings for the urls:

angular.module('app').config(function (LightboxProvider) {
  LightboxProvider.getImageUrl = function (imageUrl) {
    return imageUrl;
  };
});

angular.module('app').controller('GalleryCtrl', function ($scope, Lightbox) {
  $scope.images = ['1.jpg', '2.jpg', '3.jpg'];

  $scope.openLightboxModal = function (index) {
    Lightbox.openModal($scope.images, index);
  };
});

Setting different limits for the image and lightbox dimensions

By default, images are scaled only if they are too large for the modal to contain without scrolling. To change this behaviour, see the documentation of the following methods:

  • LightboxProvider.calculateImageDimensionLimits
  • LightboxProvider.calculateModalDimensions

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.