I have a probelm, i have created a custom theme on Shopify and do all my stuff, but now i need to open some phots in a lightbox, i have noticed that if images are not processed the lightbox work, the problem is that shopify process all images and add at the end of url of images other piece of url, for example "myimage.png" became "myimage.png?v=4234324" and thats way its not working, how can i "say" to ignore all what came after .png and no matter whats is, just put into lightbox??
This is code:
`window.addEventListener('load', function() {
document.querySelectorAll('img[src$=".png"], img[src$=".jpeg"], img[src$=".webp"], img[src$=".jpg"]').forEach(function(img) {
img.addEventListener('click', function() {
var lightbox = document.createElement('div');
lightbox.id = 'lightbox';
document.body.appendChild(lightbox);
var imgElement = document.createElement('img');
var src = this.src.split('?')[0];
imgElement.src = src;
lightbox.appendChild(imgElement);
lightbox.addEventListener('click', function() {
lightbox.parentNode.removeChild(lightbox);
});
});
});
});`
thx in advance, im going crazy
i have tried also to put like this img[src$=".webp?v"] or the full url of image, but nothing
Related
I have hosted my site on Wordpress. and recently I have been facing the problem of an image link broken in my site. I don't know when it's happened. The problem arises in the only post section.
There are some ways to do that, but if you don't know where to start, you can try this with javascript:
function findToDeleteBrokenImages() {
var images = Array.from(document.querySelectorAll("img"));
for (var image of images) {
var img = new Image();
img.onerror = function () {
image.parentElement.removeChild(image);
};
img.src = image.src;
}
};
findToDeleteBrokenImages();
<img src="foo" />
But notice that, there are serveral ways to display an image, too. So, if your image is shown as a background of some div element or canvas, you can use the same logic of the code above, but the way to remove/delete that element is different.
Let's assume a bunch of images are preloaded via something like this:
function loadNextImage() {
var img = new Image();
img.src = '/path/to/imageN.jpg';
img.onload = function() {
if(stillLoading()) {
loadNextImage();
} else {
printLoadedImages();
}
}
}
What can be used for printLoadedImages() to make them print (ultimately as a series- one per page, but for starters let's just say one image)
When I try setting the image source to a div, I can see the image- but trying printThis or printArea jquery plugins did not do it... seems to expect that the image was loaded via page load, not at runtime
I have a page that swaps some fairly large images in and out. There are too many to preload when the page initially loads so that is not an option. So what I need to do is load them as they are requested by the user. Right now I'm using jQuery to replace the img's src. This works fine but the images I am loading can be around 500KB and it looks bad as they paint down the screen as they are downloading. What I'd like to do is pop a loading gif on the page when the image is in the process of loading then have the loading gif disappear once the image is loaded. I'm struggling to find a way to do that though. Here is the JS/jQuery code that I have that just replaces the src.
var product = "bowl";
var image = "dog.jpg"; //this is actually pulled from a data attribute, but its just hardcoded here for an example
$("#images img[data-product="+product+"]").attr("src", "/img/tablesetting/"+image);
I made a working jsfiddle showing this principle
http://jsfiddle.net/kasperfish/c72RT/4/
I recently needed to do the same thing. Basically I wrapped the image in a container div. within the container I've added a span element with my ajax loader gif embedded. this span has to be hidden initially but gets visible when an ajax request is made. The span gets removed when the image is fully loaded.
before ajax call
$('#your_image_container').find('span').show();
on success
$('#your_image').attr('src', 'your/image/url').load(function() {
$('#your_image_container').find('span').fadeOut();
});
I made a jsfiddle showing this principle
http://jsfiddle.net/kasperfish/c72RT/4/
Preload the image.
var product = "bowl";
var imageSrc = "dog.jpg";
var imgEl = $("#images img[data-product="+product+"]");
// show loading graphic only if it's needed
var timer = setTimeout(function(){
imgEl.attr("src", "/img/loading.gif");
},50);
// preload image
var img = new Image();
img.onload = function() {
clearTimeout(timer);
imgEl.attr("src",imageSrc);
}
img.src = imageSrc;
$img.attr("src", newImage);
if (!$img.get(0).complete) {
$img
.hide()
.after("<img src=throbber>")
.on("load", function () {
$(this).show().next().remove();
});
}
I got a web application, in which there are some images.
Will show a overlay in my page at start and that will automatically fadeout on all images loades.
I need something like this
its rough code
var image1="image1.jpg";
var image2="image2.jpg";
var image4="image4.jpg";
image1 & image2 & image4 loaded then fadeout #preload and show content.
Please help me ... I tried this .. but not working ..
var img1 = new Image();
img1.src = "../images/wall.jpg";
img1.onload = function() {
alert("loaded");
};
var images_loading = $('img').length;
$('img').load(function(){
if(!--images_loading) {
// all images loaded
}
});
Please mind you can't use display:none to hide images.
Using display:none will block image download by your browser. User visibility:hidden instead.
Try this fiddle. I've made it using mostly raw javascript.
If you want, you could use jQuerys .load to replace the onload function, and append to replace appendChild
I'm working on a photo gallery, and I would like the ability to have a gif preloader show before the main image is loaded, and then show the main image.
So, this is what I got:
I have a #photo_place which is the div that holds the main photo. This changes depending on what thumbnail the user selects. When the user does select a thumbnail, this function is triggered:
function gallery(icon){
$('#photo_place').css('background-image','url("../images/'+icon+'.png")');
}
Now, what I want, is to first, show a preloader gif at the #photo_place, load in the selected image ... somewhere, and when that image is loaded, replace the preloader, with the main image.
So maybe something like this?
function gallery(icon){
$('#photo_place').css('background-image','url("../images/preloader.gif")');
load.in.image'images/'+icon+'.png';
if(load.in.image == true){
$('#photo_place').css('background-image','url("loaded image")');
}
}
Of course, that wasn't real JS, but something like that should work right?
Any ideas?
Thanks
Perhaps you're looking for something like this?
function gallery(icon) {
var preLoader = '../images/preloader.gif';
var imagePath = '../images/' + icon + '.png';
$('#photo_place').css('background-image','url("../images/preloader.gif")');
var image = new Image();
image.onload = function() {
$('#photo_place').css('background-image', imagePath);
}
image.src = imagePath;
}
Also checkout Image class reference: http://www.javascriptkit.com/jsref/image.shtml