Before/After image swap using Fancybox and Cloudzoom - javascript

I'm trying to get Fancybox working with an image swap script that shows the "before" image of a retouched photo in the main large image area when fancybox is active.
Before fancybox is active I currently also have cloudzoom running on the retouched image, then when the retouched image is clicked it opens fancybox. Now when the user clicks or hovers over the middle of the active fancybox image, I want the image to swap to the before image the has not been retouched.
You can see an example of the site here: http://www.pixlsnap.com/j/testb.php
and an example of the running image swap script here: http://brecksargent.com/swaptest.php
The images are being loaded using the slideshowpro director API.
Here is the script I am trying to implement with fancybox:
$(document).ready(function() {
$(".fancybox-image").hover(
function(){this.src = this.src.replace("_off","_on");},
function(){this.src = this.src.replace("_on","_off");
});
var imgSwap = [];
$(".fancybox-image").each(function(){
imgUrl = this.src.replace("_off","_on");
imgSwap.push(imgUrl);
});
$(imgSwap).preload();
});
$.fn.preload = function() {
this.each(function(){
$('<img/>')[0].src = this;
});
}
This is the code that binds a click event to the cloudzoom image area:
$(function(){
// Bind a click event to a Cloud Zoom instance.
$('#1').bind('click',function(){
// On click, get the Cloud Zoom object,
var cloudZoom = $(this).data('CloudZoom');
// Close the zoom window (from 2.1 rev 1211291557)
cloudZoom.closeZoom();
// and pass Cloud Zoom's image list to Fancy Box.
$.fancybox.open(cloudZoom.getGalleryList());
return false;
});
});

Figured it out!
Used this
<script type="text/JavaScript">
// prepare the form when the DOM is ready
$(document).ready(function() {
$(".img-swap").hover(
function(){this.src = this.src.replace("_on","_off");},
function(){this.src = this.src.replace("_off","_on");
});
var imgSwap = [];
$(".img-swap").each(function(){
imgUrl = this.src.replace("_off","_on");
imgSwap.push(imgUrl);
});
$(imgSwap).preload();
});
$.fn.preload = function() {
this.each(function(){
$('<img/>')[0].src = this;
});
}
</script>
changed this
$.fancybox.open(cloudZoom.getGalleryList());
return false;
});
to this
$.fancybox({
afterShow: function () {
$(this.content).attr("tabindex",1)
}
});
and put each image in it's own div with img-swap class:
<div class="hover" id="hover6" style="display: none"><img src="image_on.jpg" class="img-swap" /></div>

Related

How can I fade out one img then fade in another?

So I am using jQuery to fade in and out but I encountered the problem in which when I run the code the img is faded out but the same img fades back in. I am stuck.
Here is the code
$(document).ready(function() {
// preload the image for each link
$("#image_list a").each(function() {
var swappedImage = new Image();
swappedImage.src = $(this).attr("href");
});
//-------------------------
// set up the event handlers for each link
$("#image_list a").click(function(evt) {
// alert($(this).attr("title"))
$("#image, #caption").fadeOut(3000, function(){
$("#image").attr("src", $(this).attr("href"));
$("#caption").text($(this).attr("title"));
});
$("#image, #caption").fadeIn(3000);
evt.preventDefault();
});
// get the image URL and caption for each image and animate the caption
// cancel the default action of each link
// move the focus to the first link
}); // end ready

Preload image on event

How can I preload image only when event starts, i.e. .scroll or .click ?
What happens now is, image loads along with website, and I want to prevent this from happening.
Thanks.
Use .one() , .appendTo()
$(element).one("click", function() {
$("<img src=/path/to/img/>").appendTo(/* target element */)
})
$(window).one("scroll", function() {
$("<img src=/path/to/img/>").appendTo(/* target element */)
})
Maybe something like this:
$(function() {
$('img').each(function() {
var self = $(this);
self.attr('data-src', self.attr('src'));
self.removeAttr('src');
});
var loaded = false;
function loadImages() {
if (!loaded) {
$('img').each(function() {
var self = $(this);
self.attr('src', self.data('src'));
});
loaded = true;
}
}
$('button').click(loadImages);
$(window).scroll(function() {
loadImages();
$(this).unbind('scroll');
});
});
if the javascript is executed after images are loaded you can try to change img src to data-src in html file.
You could create an image tag with the source in an data attribute:
<img data-src="your_image.jpg">
And then load it on an event:
$('body').on('click', function(){
$('img[data-src]').each(function(i, img){
$img = $(img);
$img.attr('src', $img.data('src'));
$img.removeAttr('data-src');
});
});
This should work
$(function(){
$(document).one("scroll, click", function(){
loadImages();
})
})
Here loadImage is a function which will load your images on click/scroll event. "one" method will make sure that it only happens only once else you will end up loading images every time someone click or scroll.
var src = 'location/file.jpg';
$(document).on('click', function(){
$('target').append('<img src="' +src+ '" />');
});

Why jquery plugin is not repeating after it's been executed

I am struggling to find a solution to repeat action when user did already one time. For example, when user comes on site there is carousel which can be swiped, slided, and eventually opened in order to see full gallery. I have managed to do that but when the user made this action and closes modal box, he is not able to repeat the same action. Anyone has idea how to solve it?
Here is js
$(document).ready(function () {
// - jQuery
// Swipe feature
$('#myCarousel').swiperight(function () {
$('#myCarousel').carousel('prev');
});
$('#myCarousel').swipeleft(function () {
$('#myCarousel').carousel('next');
});
// Swipe feature for modal
$('#modal-carousel').swiperight(function () {
$('#modal-carousel').carousel('prev');
console.log('swipe_right');
});
$('#modal-carousel').swipeleft(function () {
$('#modal-carousel').carousel('next');
console.log('swipe_left');
});
// Additional toolbar with "close" button appears
$(window).on('shown.bs.modal', function () {
$('modal').modal('show');
$('#toolbar').css('display', 'block'); //.css('position','fixed')
$('.carousel-inner').css('overflow', 'visible');
// Remove entirely in final version
$('.item').css('background', 'none')
console.log('fires toolbar');
});
$(window).on('hidden.bs.modal', function () {
$('modal').modal('show');
$('#toolbar').css('display', 'none'); //.css('position','absolute')
$('.carousel-inner').css('overflow', 'hidden');
// Remove entirely in final version
$('.item').css('background', '#60b1ff')
console.log('closes toolbar when modal is closed');
});
// Closes modal on toolbar
$('.trigger').click(function () {
$('.modal').modal('hide');
});
/* Modal Carousel from version 3. adjusted to version 2.3 */
/* activate the carousel */
$("#modal-carousel").carousel({
interval : false,
});
/* when clicking a image */
$(".thumbnail").click(function () {
var content = $(".carousel-inner");
content.empty();
var id = this.id;
var repo = $('#img-repo .item');
var repoCopy = repo.filter('#' + id).clone();
var active = repoCopy.first();
var next = repoCopy.last();
active.addClass('active');
content.append(repoCopy);
// show the modal
$('#modal-gallery').modal('show');
});
// Carousel pause - prevents automatic slideshow
$('.carousel').carousel({
pause : true,
interval : false
});
// Close window
$('.modal-backdrop').modal({
show : false
});
}); //end of script
Problem is at the content.empty(); which after executed action stays empty. Any ideas how to solve this?
Fiddle
Fiddle 2 with working carousel. Just imagine that you are able to open one of those images in modal.

JS: Slide open multiple divs

So I found a script to slide open multiple divs but I can't seem to get it to work. I thought it was something wrong with my main page, but I created a separate page and still can't seem to get it to work.
No messages in debugger, and no errors; but the DIV doesn't appear.
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type='text/javascript'>
(function ($) {
$.fn.showHide = function (options) {
//default vars for the plugin
var defaults = {
speed: 1000,
easing: '',
changeText: 0,
showText: 'Show',
hideText: 'Hide'
};
var options = $.extend(defaults, options);
$(this).click(function () {
// optionally add the class .toggleDiv to each div you want to automatically close
$('.toggleDiv').slideUp(options.speed, options.easing);
// this var stores which button you've clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr('rel');
// here we toggle show/hide the correct div at the right speed and using which easing effect
$(toggleDiv).slideToggle(options.speed, options.easing, function() {
// this only fires once the animation is completed
if(options.changeText==1){
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
}
});
return false;
});
};
})(jQuery);
</script>
<a class="show_hide" href="#" rel="#slidingDiv">View</a></pre>
<div id="slidingDiv" class="toggleDiv" style="display: none;">Fill this space with really interesting content.</div>
It is working, for me. You forgot to call function showHide(), actually, and you can do it on this way:
$(document).ready(function() {
$('.show_hide').showHide();
});
http://jsbin.com/amuhom/1/edit
I have added one div and one button more. I guess this is expected behavior?

hide title attribute from link on mouse hover with javascript/jquery

I am using the title attribute in all my links but I don't want to be visible on mouse hover, but still visible in the code for screen readers.
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (links[i].className == 'suppress') {
links[i]._title = links[i].title;
links[i].onmouseover = function() {
this.title = '';
}
links[i].onmouseout = function() {
this.title = this._title;
}
Using jQuery, you can hide the title attr on hover, and replace it on mouseout:
$(function(){
$('a').hover(function(e){
$(this).attr('data-title', $(this).attr('title'));
$(this).removeAttr('title');
},
function(e){
$(this).attr('title', $(this).attr('data-title'));
});​
});
Like in this fiddle.
Since you are using jQuery, you can do it in a simple way:
$(document).ready(function(){
$("a").removeAttr("title");
});
Or, setting it to empty value:
$(document).ready(function(){
$("a").attr("title", "");
});
If this is gonna change the way the screen readers read, then, you can just target on hover.
$(document).ready(function(){
$("a").hover(function(){
$(this).attr("rel", $(this).attr("title"));
$(this).removeAttr("title");
}, function(){
$(this).attr("title", $(this).attr("rel"));
$(this).removeAttr("rel");
});
});
I was trying to create a bubble tool-tip in CSS but the browser tool-tip would always appear and mess things up. So, like you, I needed to disable the default tool-tip.
I used a bit of JQuery to remove the "Title" tag but only on mouse hover. As soon as the mouse is out, the "Title" tag is restored.
Following is a DIV with some "Title" content:
<div class="tooltip-class" title="This is some information for our tooltip.">
This is a test
</div>
Now you can run the following JQuery to hide the Title tag on mouse hover:
$(document).ready(function(){
$(".tooltip-class").hover(function(){
$(this).attr("tooltip-data", $(this).attr("title"));
$(this).removeAttr("title");
}, function(){
$(this).attr("title", $(this).attr("tooltip-data"));
$(this).removeAttr("tooltip-data");
});
});
Following is a link to the full example:
http://jsfiddle.net/eliasb/emG6E/54/
$(".element").hover(function(){
// Get the current title
var title = $(this).attr("title");
// Store it in a temporary attribute
$(this).attr("tmp_title", title);
// Set the title to nothing so we don't see the tooltips
$(this).attr("title","");
});
$(".element").click(function(){// Fired when we leave the element
// Retrieve the title from the temporary attribute
var title = $(this).attr("tmp_title");
// Return the title to what it was
$(this).attr("title", title);
});

Categories