Replace images in PhotoSwipe gallery - javascript

I have a PhotoSwipe gallery on my page which is created programatically like this:
var instance = window.Code.PhotoSwipe.attach(image, options)
Now I want to update the images in the gallery, or put a new gallery in the same spot.
Creating a new gallery for the same DOM Element omits the following error:
Code.PhotoSwipe.activateInstance:
Unable to active instance as another instance is already active for this target
Detaching the instance from the Element using Code.PhotoSwipe.detatch(instance) didn't help either.
Any ideas how to fill the gallery with new images, or remove it, so I can create a new one in the same place?

The only way I found to avoid that error was calling unsetActivateInstance before detatch:
window.Code.PhotoSwipe.unsetActivateInstance(instance);
window.Code.PhotoSwipe.detatch(instance);

you can also hide the current active instance, instead of detatching it by calling
window.Code.PhotoSwipe.activeInstances[0].instance.hide(0)

I tried all the suggestions above but none of them worked well.
So my solution was to simply never create the gallery for the same ID twice:
instance = window.Code.PhotoSwipe.getInstance(myuniqueid);
if (window.Code.Util.isNothing(instance)) {
// Only initialize if there is no gallery with this ID already.
myPhotoSwipe = window.Code.PhotoSwipe.attach(window.document.querySelectorAll(galleryimagesselector), {captionAndToolbarAutoHideDelay: 0, jQueryMobile: true, preventSlideshow: true, enableMouseWheel: false, enableKeyboard: false}, myuniqueid);
}

Related

Use full page multiple times in different tabs

As you can see I have different tabs. I want to use the full-page method for each group( Each group has its own section slide ) separately. As I can use the full page only once I'm stuck. Can you tell me how to do this?
It's working fine for the first group but not working on the second. I also tried the destroy method mentioned in the documentation of fullpage.js
You can destory and initialisee fullpage.js every time you need a new instance.
Use the fullpage_api.destory('all') method and then initialise it again.
It should look kind of like this:
function showTab1(){
fullpage_api.destroy('all');
// loading HTML for tab1
loadHTML();
new fullpage('#fullpage', options);
}
function showTab2(){
fullpage_api.destroy('all');
// loading HTML for tab2
loadHTML();
new fullpage('#fullpage', options);
}

jquery owl-carousel owl-dots class issue

I get some problem when I use owl-carousel in my Rails project.
when I go back to the cached page of my browsers which is using carousel class, I get too many carousel owl-dot classes in my page,this is my
JS code
function initScrollboxHobby() {
var owl = $(".owl-carousel");
owl.owlCarousel({
// loop: true,
items: 1,
nav: true
});
}
and issue HTML code
How to fix it ?
I guess you are using turboinks and when you go back the page is cached by it and when it loads it reruns the owl carousel init function.
The problem, básically, is that turbolinks doesn't play well with non-idempotent functions https://github.com/turbolinks/turbolinks#making-transformations-idempotent
I've managed to make a workaround for this problem with other js plugins, so it should work for you.
Básically, the idea is:
First, when the user enters the page for the first time, copy the content of the element with class .owl-carousel on itself as a data attribute
carousel = $('.owl-carousel');
carousel.data('originalHtml', carousel.html();
carousel.owlCarousel(....)
Then, when the users goes back, before initializing the carousel, check if it was initialized and cached, in that case, first replace the content with the original and remove classes
carousel = $('.owl-carousel');
if (carousel.hasClass('owl-loaded')) {
carousel.html(carousel.data('originalHTML')).removeClass('owl-theme owl-loaded owl-drag');
}
carousel.owlCarousel(....)
You can mix both steps into one:
$(function() {
const carousel = $('.owl-carousel');
if (carousel.hasClass('owl-loaded')) { //if it has the class then it means it's the cached view
carousel.html(carousel.data('originalHTML')).removeClass('owl-theme owl-loaded owl-drag');
} else { // else it's a fresh load of the page
carousel.data('originalHtml', carousel.html());
}
carousel.owlCarousel(....)
})
It's a little bit hacky, but the only way I found to use plugins that are not prepared to work with turbolinks without going through modifying those plugins.
Another option would be to just disable Turbolinks if you thing you just don't need it.
For arieljuod's help of problem reason and my own trying.
I coded like this now.
var owl = $(".owl-carousel");
var owl_navs = $('.owl-carousel .owl-nav');
var owl_dots = $('.owl-carousel .owl-dots');
var owl_cloned = $('.owl-carousel .owl-stage-outer .owl-stage .cloned');
if(owl.hasClass('owl-loaded'))
{
owl_navs.remove();
owl_dots.remove();
owl_cloned.remove();
}
owl.owlCarousel({
loop: true,
items: 1,
nav: true
});
It's tedious but worked well.
Now,I understanded reason.
When I go back to cached page,because I wrote javascript code in my ERB file,in that way, old HTML code may changed.
And then Turbolinks function will run the JS code in that CHANGED new HTML code,Turbolinks must do that,because when I visit cached page again,it will lose all the event binds.
So the whole carousel items will be messy.

iDangero Swiper using Worklight?

Is there an easy way to implement this? I am having trouble getting it to work properly. I tried following the instructions on the website to have it load at window.onload, but Worklight seems to fire that call before the DOM is even visible, so I couldn't do that. I have some initialization code and so I am calling the following method in that code:
function runSwiper(){
// iDangerous Swiper
var mySwiper = new Swiper('.swiper-container', {
pagination : '.pagination',
loop : true,
grabCursor : true,
simulateTouch : true,
paginationClickable : true
});
}
Yet, all I see in my carousel is just Text. I have two test slides in there currently, but the slide doesn't even seem to work on my Android emulator. Is there anything else I need to be doing for this to work properly? If not, is there something that works as nicely as this that will play nice with Worklight?
I am using worklight version 6.2. I am usingth is: http://www.idangero.us/sliders/swiper/api.php
What I am aiming to do is fade out one div that has information, and fade in another div that has more info and the Swiper present, so initially this swiper will be hidden.
I turns out, in my case, the easiest way to call the swiper to initialize was later on (being that it was in a hidden div) instead of at window.onload. In addition, I had some issues with slides disappearing, which I fixed by having the slides pre-defined in the HTML instead of adding them dynamically.

jQuery - bxSlider plugin reloadSlider issues

I'm using jQuery with the bxSlider plugin, here is the link to it just incase: http://bxslider.com/
I'm trying to reload the slider and my custom pager after I've removed certain slides from it.
Here is what I have tried:
$(function() {
var slider = $('#slider').bxSlider({
pagerCustom: '#bx-pager'
});
$('.list').on('click', '.delete', function() {
image = $(this).closest('li').find('[type="hidden"]');
// image.attr('id') contains a string: image-0, image-1, image-2, etc.
$('#slider, #bx-pager').find('.' + image.attr('id')).remove();
slider.reloadSlider({
pagerCustom: '#bx-pager'
}); // I have also tried: slider.reloadSlider();
});
});
It works partially. What happens is the slider gets reloaded just fine but it removes the pager completely when it runs the reload.
Thank you very much for any help.
As long as I see, this is a bug in bxSlider, in fact, when you call the reloadSlider method, internally are called the methods destroySlider and init.
In the destroySlider method the pagerEl element is destroyed, this is right if you are not using a custom one, because it is recreated programmatically in the init method, but if you are using a custom one it can't be recreated programmatically.
I ended up modifying the destroySlider method to check if a custom pager is used, in this case it must not be deleted.
Here is the before (line 1294):
if(slider.pagerEl) slider.pagerEl.remove();
And after:
if (slider.settings.pagerCustom === '') {
if(slider.pagerEl) slider.pagerEl.remove();
}
I'll post the bug on GitHub as soon as I have the time.

load images dynamically into jquery iviewer

Anyone ever loaded images dynamically into jquery iViewer? Currently, it's setup as
$(document).ready(function(){
$("#images").iviewer(
{
src: "images/voices/big/page01.jpg",
update_on_resize: false,
initCallback: function (object)
{
$("#in").click(function(){ object.zoom(1);});
$("#out").click(function(){ object.zoom(-1);});
$("#fit").click(function(){ object.fit();});
$("#orig").click(function(){ object.toOrig();});
$("#update").click(function(){ object.update();});
}
});
});
so you have to put different instances on the page in order to show more than one image. Currently i have the "scrollable" plugin for thumbnails and i wanted to be able to dynamically load their large images into iViewer plugin. Anyone got any ideas?
Current example here: http://orangepxl.com/yasuko/voices.php only 1 image will work... :(
Please, take the last version of the plugin from the github. Iviewer is now a jQuery UI widget and it has loadImage method, so you can show different images in a single widget.
You could load new image with code like this:
jQuery('.iviewer_selector').iviewer('loadImage', 'http://url.to/image.png');
See https://github.com/can3p/iviewer for examples.
You can load All of them and, can hide them using jquery, and show them according to the click event for Thumbnails.
Do the iViewer for all of the images and hide them using jquery or just css,
and when clicking on the thumbnails, show only the corresponding image, and hide rest

Categories

Resources