Load images before jQuery function ask for them [duplicate] - javascript

This question already has answers here:
Preloading images with jQuery
(20 answers)
Closed 6 years ago.
In my wordpress site i am using a quite simple jQuery piece of code:
jQuery(".first-sq").mouseenter(function(){
jQuery(".first-sq img").attr('srcset', '/wp-content/uploads/2017/01/MachineLearning_hoverx2.png');
})
jQuery(".first-sq").mouseleave(function(){
jQuery(".first-sq img").attr('srcset' , '/wp-content/uploads/2017/01/MachineLearningx2.png');
})
Now the code works but the problem is when mouseenter called it takes some time for the image to load, and you can see it being loaded. or in other words, the image revealed in portions. Is there a way to load all the images the document might use ,when document load, so when in situations like my mouseenter the image will show immediately and wont have to load?

You could achieve this by preload images. Try something like that:
$(document).ready(function () {
$('<img src="/wp-content/uploads/2017/01/MachineLearning_hoverx2.png">');
$('<img src="/wp-content/uploads/2017/01/MachineLearningx2.png">');
});

Copy of this
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
});
}
// Usage:
preload([
'img/img1.jpg'
]);

Related

jQuery working in Console but not working in js file [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 2 years ago.
I am using the following jQuery code in the search bar of my website. Since I am a beginner, I am unable to relate to other code examples, hence I need help in fixing the code.
jQuery(document).ready(function ($) {
//Open Link in Search Results in New Window
jQuery('div.search_result_item_content').click(function () {
console.log("I am executed");
var menuLink = $('div.col.col-9.search_result_item_content').data('link');
window.open(menuLink, "_blank");
});
});
The code without .ready() seems to work on the console. But not on the index.js file of my website. The same file has many other jQuery functions, which are all working smoothly.
Any idea, what is causing the issue?
You are generating the items dynamically so this will not work. What you can do is what I will do below.
jQuery(document).ready(function ($) {
jQuery(document).on('click','div.search_result_item_content',function () {
console.log("I am executed");
var menuLink = $(this).data('link');
window.open(menuLink, "_blank");
});
That should work for you.

JS onpage/window load [duplicate]

This question already has answers here:
How to run a function when the page is loaded?
(11 answers)
Closed 6 years ago.
I want to run this script on pageload or after all the elements are loaded.
JavaScript
<script type="text/javascript">
function backgroundload (){
$(".portfolio-background-color")
var color = /#[0-9\A-F]+/.exec($(this).html())[0];
$(this).css('background', color)
}
window.onload = backgroundload;
</script>
i'm new to js please check if my code is okay and is it the correct way to load the js
All Javascript runs on page load. If what you mean is that you want it to run after all the elements in the page have been initialized, there are several ways:
window.onload
document.onload
body.onload
$(document).ready
There are more in-depth explanations of the support for the first three, and the differences between them, here. Documentation for $(document).ready is here.
However, in my experience, the easiest way to ensure that a script runs after all synchronously-loaded content is simply to place the <script> element at the bottom of the <body>.

Add class to dynamically added element [duplicate]

This question already has answers here:
jquery .load( ) and trigger function AFTER new content loads? just like JavaScript onload event
(3 answers)
Closed 8 years ago.
I have a shared left menu in a separate file from my pages so that I can update it in one place.
I am loading it onto the page in a .js file like so:
$(function () {
$("#left-nav").load("templates/left-nav.html", function() {
...
});
...
}
I would like to be able to set the nav-list item for the page to be selected. Something like this:
$("#calendar").addClass("selected");
...where #calendar is the ID of a list item in my left-nav.html template file.
Everything I've read says that I have to call addClass as a callback after I load in the file, but the following doesn't seem to work:
$(function () {
$("#left-nav").load("templates/left-nav.html", function() {
$("#calendar").addClass("selected");
});
...
}
Question: Is it possible to add classes to dynamically added content on a page, and if so, how should I do it? Thanks!
The code is correct as written, adduming that left-nav.html does contain an element with the ID of calendar.
The completed event is fired after the HTML is added to the DOM according to the jQuery documentation.

Custom cursor on click [duplicate]

This question already has answers here:
Using jQuery or just plain Javascript is it possible to change to a custom cursor?
(2 answers)
Closed 8 years ago.
I'm working on something and I want to change the cursor with a custom gif animation. The reason for this is the loading time. When I'm clicking a link it takes about 2 seconds to load and this is why i want to have a nice loadking cursor. Is there any way to change it with the onClick()?
Thanks for your time!
Edit: Because the loading occures between the 2 pages is it possible to change the loading mouse too?
You can do that like this;
jQuery(document).ready(function(){
jQuery('a.cursor_wait').click(function(){
jQuery('body').css('cursor', 'wait');
$(this).css('cursor', 'wait');
});
});
jsfiddle link

jQuery "$(document).ready(function () {" equivalent in Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
$(document).ready equivalent without jQuery
I want to wait until an ASP.NET datagrid is resized correctly before showing a popup message. This works great:
$(document).ready(function () { showpopup(); });
But I need to acheive it without jQuery. I've tried many ways:
$(window).bind("load", function() {showpopup();}
$(function() { showPopup();}
but this doesn't work.
My preferred method of doing such things is to start my script with:
var loadScripts = [],
loadScript = function(callback) {loadScripts.push(callback);
Then, the very last thing on the page before </body> is:
<script type="text/javascript">(function() {var x; while(x=loadScripts.shift()) x();})();</script>
Then, whenever there's something I want to defer until the DOM has loaded, I simply enclose it in:
loadScript(function() {
// do stuff here
});

Categories