I have made a little plugin where I get the size using naturalHeight and naturalWidth
Sometimes when loading page I get 0 in width and height (other times it works fine).
I guess it is because it calculate the size before loading the image.
How can I correct this?
I have tried to run plugin $("selector").myplugin() inside $(document).ready, but that doesn't change anything
I have also tried to wrap setTimeout around the variables where I am using naturalHeight and naturalWidth, but that doesn't prevent rest of the code to run - and I won't make it slower loading than necessary (wrap everything in setTimeout if not necessary)
So my question - are there a way to
if size is 0 - wait a little and try again
Not continue running rest until it is not 0 anymore
Or does anyone have another solution?
Ready wait only for dom to be loaded , You can use the load() on your image in order in this case , you'll not wait till all the window elm are loaded
See below snippet after loading image , The size of this last is being printed on the bottom ,
$(function(){
$( "#img" ).load(function() {
console.log($(this).width()+"-"+$(this).height())
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="img" src="https://cdn.pixabay.com/photo/2017/09/07/14/15/wide-2725426_960_720.jpg" >
Related
So I am using the slick image slider and everything was working great until I considered what would happen if images of different heights were put in. I then wrote a short piece of js to check which image is the tallest and set the heights of all of them to that height.
window.onload = imageSlideChange;
$(window).resize(imageSlideChange);
function imageSlideChange(){
var tallestImage = $('.slick-slide').first().height();
$('.slick-slide').each(function(){ //.slick-slide is the class of each image
if($(this).height() > tallestImage){
tallestImage = $(this).height();
}
});
$('.slick-slide').each(function(){
$(this).height(tallestImage);
});
}
The weird thing though is the code only runs correctly when the slider is in the browser window. If I scroll to the bottom or top of the page and reload it will only load the images as 1px height. I thought maybe it was the images not being loaded and set the function to only run on window.loadbut beyond that I don't know what could cause this kind of behavior.
When you go to inspect the images they are the correct height. The 1px must be coming from their default height, which means that when the code runs it must be reading their heights as 0. Why???
If it helps this is all happening as drupal serves up the images, so could that be the problem?
I wrote a jQuery script which keeps two images side by the same height. It needs to work on document load and document resize as the images are responsive.
I thought this woas the best solution (code below), but it only works when I resize the browser?
function projectRowHeight() {
var rowHeight = $('#projects').find('.col-33 img').height();
$('#projects').find('.row').css('height', rowHeight);
}
$(document).ready(projectRowHeight);
$(window).resize(projectRowHeight);
Use $(window).load(projectRowHeight);
When I load my page and use $('div.logo').width() it returns a massive value of 6500, which is incorrect. The image is only 130 px wide.
But, if I use a setTimeout and check the $('div.logo').width() after a few seconds it returns the correct width.
Why would this be? All of my CSS should be already loaded so why would the few second pause make a difference?
You have to check the image width after the image has been loaded.
So either:
// NOTE: not $(document).ready
$(window).load(function() {
var width = $('.logo img').width();
});
Or
$('.logo img').load(function() {
var width = $(this).width();
});
should give the right width.
Set the width and height attributes to the image. You can specify the size in CSS too. Then the browser will know what size the image will be before it loads.
That's because an image is not loaded yet, so initial size is based on CSS or guessed. You can use:
$('.logo img').load(function() {
var width = $(this).width();
});
Image is getting loaded with GET method,
so you need to check it after,
$('.logo img').load(function(){
//your code
});
If you want your image gets loaded instantly you may go with BASE64 image.
For more detail Plase see here
it can be javascript modifying the layout of page, for example it can be script tags included on the bottom of the page, or any other javascript code executed on JQuery ready or load events. For example if this code appending html code it can affect width of you .logo div.
Another possibility is that your html is incorrect, to check this simply run any HTML validator like this http://www.freeformatter.com/html-validator.html. When taking HTML from your page for validation, be aware that some browsers fix the incorrect HTML, so better get it from your source code.
I tried all the solution given on stackoverflow.
Jquery LazyLoad.js Issue with Loading after Window Resize
Lazy Load won't load visible images
jQuery LazyLoad do not load images until scroll
LazyLoad images not appearing until after a scroll
Add a width height in css
Add a width height in HTML
$(window).resize();
.trigger("lazyload");
skip_invisible:true
failure_limit : 1000
I also tried an alternative http://luis-almeida.github.io/unveil/
But the problem still the same. Unless I resize the window, (or $(window).resize(); in the console) picture don't show.
However, if I put a threshold of 300, the pictures in the 300 first pixels will appear, not the others...
The most strange is that the problem is the same for the 2 plugins.
Any suggestion?
This is an issue because your content, which contains the images, is loaded after the lazyload
Try calling lazyload in setInterval function or at the end of window.load.
var interval = setInterval(function(){
$("img.lazy").lazyload();
clearInterval(interval);
},1000);
Is it possible to load a background-image asynchronously?
I've seen many jQuery plugins to load normal image in an asynchronous way, but I can't find if it's possible to preload / asynchronously load a background-image.
EDIT
I clarify my problem. I've been working on this test site http://mentalfaps.com/
The background image is loaded randomly from a set of images refreshed each hour by a chron job (which takes random images on a flickr catalog).
The host is free and slow at the moment, so the background image takes some time to load.
The positioning of the first overlay (the one with the PNG transparent mentalfaps logo) and width are regulated by a function created in the jQuery(document).ready construct.
If you try to refresh the page many times, the width of the right overlay div is 0 (and so you see a "hole" in the layout)
Here is the method to set my positions:
function setPositions(){
var oH = $('#overlay-header');
var overlayHeaderOffset = oH.offset();
var overlayRightWidth = $(window).width() - (overlayHeaderOffset.left + oH.width());
if (overlayRightWidth >= 0) {
$('#overlay-right').width(overlayRightWidth);
} else {
$('#overlay-right').width(0);
}
var lW = $('#loader-wrapper');
lW.offset({
left: (overlayHeaderOffset.left + oH.width() - lW.width())
});
}
The problem is that the $(window).width() is lower then the effective window width! so the check fails and the script goes for $('#overlay-right').width(0);
any ideas?
Not sure whether I really understand your question, but background images (and all other images) are already loaded asynchronously - the browser will start separate requests for them as soon as it encounters the URL while parsing the HTML.
See this excellent answer for background on loading order: Load and execution sequence of a web page?
If you meant something else, please clarify.
The trick to loading something in the background is to load it once, so the next time when it is loaded it already is in the cache.
Put the following at the end of your html:
<script>
var img = new Image();
img.onload = function () {
document.getElementsByTagName('body')[0].style.backgroundImage = 'background.png';
};
img.src = 'background.png';
</script>
You could use a prefetch link in the head.
<link rel="prefetch" href="/images/background.jpg">
You should be able to add these links to the head via JavaScript.
I like to use CSS to fill the background with a color for page load.
After DOM ready event, I use jQuery to modify the CSS and add a background image. That way, the image isn't loaded until after page loads. Not sure about async, but this method gives the user a decent experience.
Example: http://it.highpoint.edu/
The right side navigation links have a background image. The page initializes with a background color. It is replaced with a background image after page load, via jQuery.
changes in this file jquery.ImageOverlay.js
set your height and width and enjoy this...
imageContainer.css({
width : "299px",
height : "138px",
borderColor : hrefOpts.border_color
});
As it is already mentioned, the background image is loaded asynchronously. If you need to load the background image from JQuery code you may also set the addClass() method to set a CSS class or attr("style=\"background-image:url('myimage.png')\"")
Ive found the answer myself, it was a problem due to the .offset() method that gived sometimes the wrong values.
I had the write values using the .position() :
var overlayHeaderOffset = oH.position();