Reputation: 9265
Fancy box 3 documentation gives little examples regarding its AJAX functionality. I want to be able to click a button and load a "gallery" consisting of images loaded from an Ajax response.
An example I found (the last one on this page) requires hard-coding the image paths of the gallery in a hidden div; this is fine, but I rather reduce page load time by loading with AJAX.
I found something that looked promising but I'm not sure how to implement Ajax into it. Any ideas?
$.fancybox.open([
{
src : '1_b.jpg',
opts : {
caption : 'First caption'
}
},
{
src : '2_b.jpg',
opts : {
caption : 'Second caption'
}
}
], {
loop : false
});
Upvotes: 2
Views: 8549
Reputation: 9265
Turns out it was super simple:
$(document).ready(function () {
$("#test").on('click', function () {
$.ajax({
type: 'POST',
url: '/neou_cms/test/ajax_resp',
dataType: 'json',
success: function (data) {
$.fancybox.open(data);
}
});
});
});
where AJAX response is:
[{"src":"\/images\/uploads\/projects\/207002523\/m_207002523_1.jpg"},
{"src":"\/images\/uploads\/projects\/207002523\/m_207002523_2.jpg"},
{"src":"\/images\/uploads\/projects\/207002523\/m_207002523_3.jpg"},
{"src":"\/images\/uploads\/projects\/207002523\/m_207002523_4.jpg"}]
You can even add in captions and optional thumbs as long as you follow this syntax:
{
src : '1_b.jpg',
opts : {
caption : 'First caption'
}
},
Codeigniter code:
$this->load->model('backend/images_model');
$query = $this->db->get_where('projects', array('id' => '207002523'));
$images = $this->images_model->get_images($query->row()->images);
$output = array();
foreach ($images as $image) {
$output[] = array('src' => $image['main']);
}
echo json_encode($output);
exit;
If you already have one image in the href and and want to load more onto it when fancybox opens, you can do:
$(document).ready(function () {
$("[data-fancybox]").fancybox({
loop: false,
onInit: function (instance) {
$.ajax({
type: 'POST',
url: '/neou_cms/test/ajax_resp',
dataType: 'json',
success: function (data) {
$.each(data, function (index, src) {
instance.createGroup({
type: 'image',
src: src
});
});
}
});
}
});
});
$(document).ready(function () {
$("[data-fancybox]").fancybox({
loop: false,
onInit: function (instance) {
let id_album = $("[data-fancybox]").attr('id');
$.ajax({
type: 'POST',
url: './neou_cms/test/ajax_resp',
dataType: 'json',
success: function (data) {
$.each(data, function (item) {
instance.addContent({
'type': 'image',
'src': item.src
});
});
}
});
}
});
});
Upvotes: 5
Reputation: 1657
There are two steps in the ajax gallery:
Link to ajax gallery:
If you look at the documentation again (ajax part):
http://fancyapps.com/fancybox/3/docs/#ajax
It states that you can easily link to a ajax gallery (no javascript required)
using data-type="ajax"
and data-src="my_page.com/path/to/ajax/"
for your ajax content
<a data-fancybox data-type="ajax" data-src="my_page.com/path/to/ajax/" href="javascript:;">
AJAX content
</a>
After you create a link to the gallery now you can move on to setting up the ajax gallery in another url.
Construct your gallery in a different url:
In your separate url (my_page.com/path/to/ajax/
) where you intend to keep all your gallery images, you just need to set up it up like how a normal fancybox gallery should be.
Example:
<div>
<a data-fancybox="ajax-gallery-1" href="http://farm8.staticflickr.com/7367/16426879675_e32ac817a8_b.jpg" title="Codirosso spazzacamino (Massimo Greco _Foligno)"><img width="160" height="106" src="http://farm8.staticflickr.com/7367/16426879675_e32ac817a8_m.jpg" alt="" /></a>
<a data-fancybox="ajax-gallery-1" href="http://farm6.staticflickr.com/5612/15344856989_449794889d_b.jpg" title="Morning Twilight (Jose Hamra Images)"><img width="160" height="106" src="http://farm6.staticflickr.com/5612/15344856989_449794889d_m.jpg" alt="" /></a>
<a data-fancybox="ajax-gallery-1" href="http://farm8.staticflickr.com/7289/16207238089_0124105172_b.jpg" title=" (Eric Goncalves (cathing up again!))"><img width="160" height="106" src="http://farm8.staticflickr.com/7289/16207238089_0124105172_m.jpg" alt="" /></a>
<a data-fancybox="ajax-gallery-1" href="http://farm9.staticflickr.com/8568/16388772452_f4d77a92c7_b.jpg" title="Arctic Paradise (Tom Draxler)"><img width="160" height="106" src="http://farm9.staticflickr.com/8568/16388772452_f4d77a92c7_m.jpg" alt="" /></a>
<a data-fancybox="ajax-gallery-1" href="http://farm8.staticflickr.com/7308/15783866983_27160395b9_b.jpg" title="Rodeo Dusk (_JonathanMitchellPhotography_)"><img width="160" height="106" src="http://farm8.staticflickr.com/7308/15783866983_27160395b9_m.jpg" alt="" /></a>
<a data-fancybox="ajax-gallery-1" href="http://farm3.staticflickr.com/2880/10346743894_0cfda8ff7a_b.jpg" title="Les papillons ont du chagrin (JMS')"><img width="160" height="106" src="http://farm3.staticflickr.com/2880/10346743894_0cfda8ff7a_m.jpg" alt="" /></a>
</div>
There is an ajax request example in FancyBox3 website as well, if you visit http://fancyapps.com/fancybox/3/, scroll to the part where it says Ajax request, you can see how the developer set up his gallery and how he display his gallery in a different url http://fancyapps.com/fancybox/3/ajax.php?v=1508722146.
Upvotes: 0