0

I'm using this ScalableLightbox jQuery plugin. When I close my lightbox the page scrolls back to the top. Is there a way to prevent this? You can see the same behavior with the plugin's examples.

I've tried to work with the close function but nothing happened:

$.ScalableLightbox("close", function() {
});

I would simply like to stay where I am on the page once I close the lightbox.
How can I achieve that?

Here's a demo:

$(function() {

  // initialize ScalableLightbox
  $.ScalableLightbox({
    // show what's happening in
    // the debugger console
    debug: true,

    // define the path for all
    // resources (images):
    baseImgPath: 'https://www.scalable-lightbox.com/assets/img/lightbox/',

    // define data structure via API
    api: 'https://www.scalable-lightbox.com/assets/api/data.json',

    // index module setting
    index: {
      // set index layout to masonry
      layout: "masonry",
      thumb: {
        // use the number caption (default),
        // change the number format and
        // position it on the top
        caption: "number",
        captionNumberFmt: "Image %n%",
        captionPosition: "top"
      }
    },

    // lightbox module setting
    lightbox: {
      img: {
        // we display the number in the left
        // caption and change the number format
        captionLeft: "number",
        captionNumberFmt: "%n% of %total%",
        // we do not use a caption in the center
        captionCenter: "none",
        // we use the link to the index module
        // on the right and change the link text
        captionRight: "index",
        captionIndexTxt: "Back to Overview",
        // we position the caption container on
        // top, as we did in the index module
        captionPosition: "above",
        captionVerticalMargin: 10
      }
    }
  });



  // open the index module via the
  // aforementioned text links
  $("#show-canyon-house").click(function(e) {

    // prevent the default anchor behavior
    e.preventDefault();

    // open the lightbox module
    $.ScalableLightbox("open", {
      module: "index",
      deck: 1
    });

  });

  $("#show-italpromo").click(function(e) {

    // prevent the default anchor behavior
    e.preventDefault();

    // open the lightbox module
    $.ScalableLightbox("open", {
      module: "index",
      deck: 2
    });

  });

});
body {
  background: #000;
  color: #fff;
}

body .container {
  margin: 40px auto;
  width: 360px;
  padding: 0 20px;
}

.container a,
.container a:visited {
  color: #aaa;
  border-bottom: 1px solid #aaa;
  text-decoration: none;
}

.container a:focus,
.container a:hover,
.container a:active {
  color: #fff;
  border-bottom: 0;
}

.intro-item {
  margin-bottom: 70px;
}

.intro-item h2 {
  text-align: center;
  font-size: 18px;
  text-transform: uppercase;
  margin-bottom: 10px;
}

.intro-item p {
  font-family: Times, serif;
  font-size: 20px;
  line-height: 25px;
  text-indent: 25px;
  margin: 0;
}


/***************************/


/* plugin overwrite styles */


/***************************/

.sl-index-overlay,
.sl-lightbox-overlay {
  background: #000;
  opacity: 1;
}


/* we change the item overlay to a white and reduce
the opacity to 0.4 */

.sl-index-item-overlay {
  background: #fff;
  opacity: 0.4;
}


/* the index caption should be black, half transparent
with the text aligned to the left and have a top/bottom
padding */

.sl-index-item-caption {
  background: #000;
  padding: 10px 0;
  color: #fff;
  text-align: left;
  opacity: 0.7;
}


/* change the link color for the lightbox captions */

.sl-lightbox-caption-container a,
.sl-lightbox-caption-container a:visited {
  color: #fff;
}

.sl-lightbox-caption-container a:hover {
  border: 0;
  color: #aaa;
}


/* we over extend the left and right arrows, so that
navigation outside of the lightbox container is
also possible */

.sl-lightbox-cursor.left {
  left: -50%;
  width: 100%;
}

.sl-lightbox-cursor.right {
  right: -50%;
  width: 100%;
}


/**** white cursors ****/


/* index module */

.sl-index-wrapper {
  cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-close.png'), url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-close.cur'), crosshair;
}

.sl-index-item-indicator {
  background: url('https://www.scalable-lightbox.com/assets/cursors/white/loader.gif') top left no-repeat;
  background-size: 16px 16px;
}

.lt-ie9 .sl-index-item-indicator {
  background: url('https://www.scalable-lightbox.com/assets/cursors/white/loader-small.gif') top left no-repeat;
}


/* lightbox module */

.sl-lightbox-wrapper {
  cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-close.png'), url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-close.cur'), crosshair;
}

.sl-lightbox-item-indicator {
  background: url('https://www.scalable-lightbox.com/assets/cursors/white/loader.gif') top left no-repeat;
  background-size: 16px 16px;
}

.lt-ie9 .sl-lightbox-item-indicator {
  background: url('https://www.scalable-lightbox.com/assets/cursors/white/loader-small.gif') top left no-repeat;
}

.sl-lightbox-cursor {
  background: url('https://www.scalable-lightbox.com/assets/cursors/white/blank.gif') 0 0 repeat;
}

.sl-lightbox-cursor.left {
  cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-left.png'), url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-left.cur'), w-resize;
}

.sl-lightbox-cursor.right {
  cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-right.png'), url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-right.cur'), e-resize;
}


/* retina optimization */

@media (min-resolution: 192dpi),
(-webkit-min-device-pixel-ratio: 2),
(min--moz-device-pixel-ratio: 2),
(-o-min-device-pixel-ratio: 2/1),
(min-device-pixel-ratio: 2),
(min-resolution: 2dppx) {
  .sl-index-wrapper,
  .sl-lightbox-wrapper {
    cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-close-retina.png'), crosshair;
  }
  .sl-lightbox-cursor.left {
    cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-left-retina.png'), w-resize;
  }
  .sl-lightbox-cursor.right {
    cursor: url('https://www.scalable-lightbox.com/assets/cursors/white/cursor-right-retina.png'), e-resize;
  }
}
<link href="https://www.scalable-lightbox.com/assets/jquery.scalable-lightbox.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://www.scalable-lightbox.com/assets/js/jquery.hammer-full.min.js"></script>
<script src="https://www.scalable-lightbox.com/assets/js/masonry.pkgd.min.js"></script>
<script src="https://www.scalable-lightbox.com/assets/jquery.scalable-lightbox.js"></script>

<div class="container">

  <div class="intro-item">
    <h2>Canyon House</h2>
    <p>
      A private residence based on a complex three-dimensional map of interlocking space types. This commission involved the renovation of a building which itself has several additions dating back to the 1960s. Located inside the Appia Antica Park on the outskirts
      of Rome, the building serves as the clients’ main residence, and is a space both for work and relaxation.
    </p>
    <p>
      The design uses vertical axes to section <a href="#" id="show-canyon-house">the building</a> into different space types including service areas, distribution areas, main quarters and outdoor areas, establishing a specific spatial and functional
      sequence as the owners move throughout the building.
    </p>
    <p>
      In addition, the horizontal sequence of floor levels differentiates zones for sleeping, living and work. These horizontal and vertical planes create a three-dimensional map for the house and its functions.
    </p>
  </div>

  <div class="intro-item">
    <h2>Italpromo &amp; Libardi Associati Headquarters</h2>
    <p>
      A new headquarters for the well&dash;known Italian communications agency Italpromo &amp; Libardi Associati in a converted school building in Ostiense, Rome.
    </p>
    <p>
      Ostiense is one of the few remaining industrial areas on the edge of Rome’s historic city centre and is currently undergoing extensive architectural and social regeneration. The brief for <a href="#" id="show-italpromo">this project</a> was to transform
      an existing building without losing its unique character and retaining its original listed façade.
    </p>
    <p>
      Labics’ main objective was to introduce new functions into the shell of the existing building, creating innovative ways of using the space based on the complex spatial, functional and social relationships within the contemporary workplace. The aim was
      to find the right balance between individual and shared spaces, between production and social areas.
    </p>
  </div>

</div>

View on CodePen

showdev
  • 28,454
  • 37
  • 55
  • 73
  • Are you using [this plugin](https://www.scalable-lightbox.com)? The [examples](https://www.scalable-lightbox.com/examples) on that site seem to have the same issue. – showdev Apr 25 '19 at 10:50
  • @showdev indeed! I don't get why they didn't address this... – Marco Tiberio Apr 25 '19 at 10:53
  • It seems to have something to do with the `_changeHash` function. [This](https://stackoverflow.com/questions/4715073/window-location-hash-prevent-scrolling-to-the-top/15403449) might shed some light. You might consider opening an issue on the [project's GitHub page](https://github.com/onyx1/scalable-lightbox), but it looks like it hasn't been updated in several years. – showdev Apr 25 '19 at 11:04
  • Setting `hash: false` seems to stop the scroll to top upon close. That might be a quick fix if you're not using the [hash features](https://www.scalable-lightbox.com/options#hash). Note that I'm not familiar with this plugin and there might be more appropriate solutions. – showdev Apr 25 '19 at 11:13
  • @showdev thanks so much! It actually worked! – Marco Tiberio Apr 25 '19 at 11:32

0 Answers0