is loading all background images different from <img> preloading - javascript

i have image slider . it takes time to load images and doesn't look good . is preloading will work for div background images.
or are there any way to load all div's background images on load . which i may require later .
$.get('photos.xml', function (xml)
{
$('item', xml).each(function (k)
{
frontimg.push($(this).find('frontImg').text());
stripimg.push($(this).find('stripImg').text());
arrBackImg.push($(this).find('backImg').text());
backText.push($(this).find('backTxt').text());
//frontDiv.push("<h2>"+fTitle[k]+"</h2>"+ "<h3>"+fCity[k]+"</h3><hr><h4>YEAR OF COMPLETION</h4>"+fYear[k]+"<hr><h4>LOCATION</h4>"+fLoc[k]+"<hr><h4>PROPERTY TYPE</h4>"+fType[k]+"<hr><h4>MAPLETREE'S TYPE</h4>"+fRole[k]);
frontDiv.push("");
$('#myImageFlow').append('<div id="id' + k + '" alt="div' + k + '" class="sliderImage" width="280" height="310" style="visibility:hidden"> <div class="ffrontText" id="ff' + k + '">' + frontDiv[k] + '</div><div class="borderdiv" id="b' + k + '"></div><div class="borderdiv1" id="bd' + k + '"></div><div class="reflection" id="ref' + k + '"></div> <div class="overlay" id="o' + k + '"></div></div>');
});
});

Yes, preloading will work with any image that gets put onto the page. All it does it load the image to the user's computer, then that image stays there and doesn't need to be downloaded when it's used on the page.

Related

HTML Element not being inserted

I'm working on a .NET Core project for my company where work orders are loaded from our SQL database using Entity Framework, filtered and then displayed as markers on a map through Google Maps API for our installers.
We have two types of filters: one that gets included in an Ajax POST, and one that filters locally to decrease load times and performance issues. What I'm trying to do is load the local filter items (lists that are included in the response when calling the initial Ajax POST). If the list of filter items exceeds 5 items, I want them to collapse to only 5 items and insert an anchor which expands (utilizes jQuery's toggle()) showing the rest of the items in that list.
This is the excerpt from the JavaScript function which takes care of that:
filterItems
.forEach((filterItem, i) => {
var localItem = '<label class="' + selectorContainerClass
+ ' selectorContainer" id="' + selectorContainerIdPrefix + filterItem.key
+ '"><input id="' + convertValToEng(filterItem.value)
+ '" type = "checkbox" class="filled-in navy" name="' + inputName
+ '" value="' + filterItem.key
+ '" onchange="localFilter(this, this.value)" /><span class="selector-value">'
+ filterItem.value
+ '</span> <span id="' + paramName + 'Cnt__' + filterItem.key
+ '" class="selector-count"></span></label ><br />';
document.querySelector("#" + colId).insertAdjacentHTML('beforeend', localItem);
if (i >= 5) {
$("#" + colId + " #" + selectorContainerIdPrefix + filterItem.key).addClass("collapse");
$("#" + colId + " #" + selectorContainerIdPrefix + filterItem.key).toggle(100);
$("#" + colId + " #" + selectorContainerIdPrefix + filterItem.key + " + br").toggle(100);
}
});
if (filterItems.length > 5) {
//TODO: Fix the bug here; the .filter-collapse element is not being inserted under local installers.
var newEl = '<a class="filter-collapse" onclick="toggleFilterExpand(false, this)";><i class="material-icons">expand_more</i></a>';
document.getElementById(colId).insertAdjacentHTML('beforeend', newEl);
}
I should be getting a newEl inserted under the "Installer" column (8 installers, 3 of them not being displayed), but I'm not. I've tried jQuery's after() and insertAfter() methods, but neither of those worked. newEl is being generated for the "Area" column, as it should, but for the "Installer" column it's not.
I've also tried inserting the element manually through the console window with the exact same code and it works.
Would appreciate some help with this as I feel lost regarding this issue.
Thanks.
It turned out to be a stupid mistake on my end where I was removing the element newEl from the all the other filter lists before inserting a new one to the currently iterated one.

How get image size of image loaded by lazy load

I am using lazy load plugin (jquery.lazyload.js) and I try to get the image size that it loads.
So in the display part I have:
echo '<img #img id="img-' . $i . '" name="' . $filename . '" class="lazy" data-original="'.$image.'" />';
Java Script:
<script type="text/javascript">
displaysize(img) {
console.log(img.clientWidth, img.clientHeight);
}
</script>
<script src="js/rectangle3.class.js"></script>
HTML:
<div id="imgsize">
<p id='imgsize-debug'></p>
<button id='map-download-btn' onclick='displaysize();'>Image size:</button>
<div id='imagesize'></div>
</div>
The image display is ok but when I add displaysize(img) function to the script and button, the page keep loading. I will need to get image size for calculation in my java script file,
document.getElementById("rectRecord-" + this.rectPointer).innerHTML =
"Rectangle " + (this.rectPointer + 1) + " ("
+ this.startX + ", "
+ this.startY + ", "
+ this.endX + ", "
+ this.endY + ")"
need to change to:
document.getElementById("rectRecord-" + this.rectPointer).innerHTML =
"Rectangle " + (this.rectPointer + 1) + " ("
+ (this.startX)/width + ", "
+ (this.startY)/height + ", "
+ (this.endX-this.startX)/width+" , "
+ (this.endY-this.startY)/height +""
As I said in the comments, you need to pass the image to the function in order to fetch the width/height. In this example you can click the image to get the size.
In order to use a button you would need to use a selector to target the right image you want the dimensions of.
function displaysize(img) {
console.log(img.clientWidth, img.clientHeight);
}
$(function() {
$("img.lazy").lazyload();
$(document).on('click', '.lazy', function() {
displaysize($(this)[0]);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery_lazyload/1.9.7/jquery.lazyload.js"></script>
<img class="lazy" data-original="http://appelsiini.net/img/bmw_m1_hood.jpg" />
Where is the display size button relative to your lazy loaded image?
Does each lazy load image have a display size button or is there only one lazy loaded image?
Your problem is that your displaysize method which uses a parameter called img but you do not pass any parameter when you call the function on click.
So you need to pass something to that display size method which will point it to the correct image.
Of your image is like this relative to your button:
<img id="lazyLoadedImage" />
<div id="imgsize">
<p id='imgsize-debug'></p>
<button id='map-download-btn' onclick='displaysize("lazyLoadedImage");'>Image size:</button>
<div id='imagesize'></div>
</div>
displaysize(imgID) {
console.log(document.getElementById(imgID).width + ","+ document.getElementById(imgID).height);
}
If you do not have a static set up like this depending on the questions i asked at the beginning here you will have to adjust this code. However whatever the case you will be able to do this by passing an actual reference of the image you would like to inspect to your display image size method.
According to lazy loading plugin site you need to set image dimensions before loading the image:
PRO TIP! You must set image dimensions either as width and height attributes or in CSS. Otherwise plugin might not work properly.
I see you are using PHP, so you can try to set width and height as advised in this answer.
You can also add an id to your images to make the javascript call from the button easier.
Finnaly I managed to find the width and height of the image since it is parent of the rectangular I created inside. So in the javascript file I will use:
var options = {
width: $('#' + rectangles[rectPointer].rectangleDiv.id).parent().width() + 1,
height: $('#' + rectangles[rectPointer].rectangleDiv.id).parent().height() - 3
};
Then width and height get from:
(options.width ||rectangles[rectPointer].startX) + " "
options.height ||rectangles[rectPointer].startY
then for my calculation:
Rectangle.prototype.updatePosition = function (data, options) {
this.startX = data.newStartX;
this.startY = data.newStartY;
this.endY = parseFloat(this.startY) + parseFloat(this.rectangleDiv.style.height);
this.endX = parseFloat(this.startX) + parseFloat(this.rectangleDiv.style.width);
console.log("END: "
+ parseFloat(this.startY) + parseFloat(this.rectangleDiv.style.height) + " - " + parseFloat(this.startX) + parseFloat(this.rectangleDiv.style.width));
document.getElementById("rectRecord-" + this.rectPointer).innerHTML =
"Rectangle " + (this.rectPointer + 1) + " ("
+ (options.width || this.startX) + ", "
+ (options.height || this.startY) + ", "
+ (this.startX)/(options.width || this.startX) + ", "
+ (this.startY)/(options.height || this.startY) + ")"
;
Thanks all for your help ! I have credited up points for all but couldn't accept as answer since it doesn't really be used to solve my issue.

CSS Style not being applied in Javascript

If I have the following html added statically the CSS style is applied correctly
HTML:
<li class="connector">
<a href="#">
<img src="css/images/temp/connector2.png" alt="" width="192" height="192">
<span class="sr-only">box</span>
</a>
</li>
However if I do the following in Javascript to dynamically render the HTML the CSS does not seem to get applied.
JavaScript:
element.innerHTML += "<li class='connector'>" +
"<a href = '/connector/" + i + "'>" +
"<img src ='/" + i + "/background' />" +
"<span class='sr-only'>" + $.get("/" + i + "/getName") +"</span>" +
"</a>" +
"</li>";
The list shows, but without the CSS styling.
Any suggestions?
"<span class='sr-only'>" + $.get("/" + i + "/getName") +"</span>" +
get is asynchronous method. This addition is wrong.
The problem was with how I was using the Isotope Javascript library. The way I was adding the content was bypassing it.
After reading their documentation, I was able to get it to work properly.
I used the following instead
$.get("/connectorCount", function(data){
for (var i = 0; i < data; i++)
{
var $newItems = $('<li class="connector"> <img src = "/' + i + '/background" /> <span class="sr-only">' + $.get("/" + i + "/getName") + '</span></li>');
$('.connectors').isotope('insert', $newItems);
}
refreshSearch(data);
});

On window resize event javascript on object tag

I have created Jquery tabs and embedded a JSP page on the parent page. I have called on window resize events on both pages. But the inner page function does not get invoked.
For the inner page I have an SVG element which needs to be resized every time the window is resized.
sLinkPageToJunction = '<%=base1%>' + "/InnerTabbedPage.jsp?IntersectionName=" + strIntersectionName + "&FrameName=" + strFrameName + "&ip=" + '<%=ServerIP %>' + "&port=" + '<%=ServerPORT %>' + "&InterCorridorName=" + svgfilename;
if (document.getElementById("JnLive" + strIntersectionName) == null) {
//var nextTab = $('#tabs li').size()+1;
Tabcount = $('#tabs li').size();
// create the tab
$('<li>' + strIntersectionName + ' <button class="close closeTab" align="right" "type="button">x</button></li>').appendTo('#tabs');
// create the tab content
$('<div class="tab-pane" id="tab' + strIntersectionName + '"> <div id="JnLive' + strIntersectionName + '" class="col-sm-6" style="margin-top: 1%"> </div>').appendTo('.tab-content');
var heightvalue = $(window).height();
var widthvalue = $(window).width()
//alert("---->"+heightvalue+"---->"+widthvalue);
var url = "<object id=obj'" + strIntersectionName + "' data='" + sLinkPageToJunction + "' height='" + heightvalue + "' width='" + widthvalue + "'></object>";
$("#JnLive" + strIntersectionName).html(url);
// make the new tab active
$('#tabs a:last').tab('show');
OpenedTabbedJunctionNames[Tabcount - 1] = strIntersectionName;
OpenedTabbedJunctionFrameNames[Tabcount - 1] = strFrameName;
registerCloseEvent();
}
So it creates a new tab if it does not already exist.
Now my on windowresize event on the inner page is
window.addEventListener('resize', changeframesizeJN) ;
on the outer page it is
window.addEventListener('resize', changeframesize) ;
But when I resize the window with the tab selected changeframesizeJN is not invoked.
How can I do that?
P.S: I dont know if I have put the information that is required for the question to be answered. The fact is I am not sure how to ask this question.
You give your panel a fixed width and height from what I can tell from your code. You should change the CSS of your inner page to resize with it's parent.
Alternatively you could call the inner page function on the resize of its outer parent as well as its own. Since its on the same domain you won't have any problem doing this:
window.addEventListener('resize', function() {
changeframesize();
window.frames['yourIframeName'].changeframesizeJN();
});
Just make sure you give your inner window a name so you can select it. Or use it's index but I would recommend using a name to keep it readable:
window.frames[0].changeframesizeJN();
Try adding the resize onto the object tag, like:
var url = "<object id=obj'" + strIntersectionName + "' onresize='changeframesizeJN()' data='" + sLinkPageToJunction + "' height='" + heightvalue + "' width='" + widthvalue + "'></object>";
If that works then you can bind it to the object, the same as your window, except with the object element instead.
objectElement.addEventListener('resize', changeframesizeJN) ;

JQM refresh collapsible set doesnt refresh

I need help with a little script ...
when i append content and drigger create tho content is loaded...
but then when i use $('#set').empty(); for the second time the content is loaded but no JQM style is added (just plain text)
i have read about this problem but i dont find any solution ... i guess that $('#set').collapsibleset('refresh');as it should... any idea ?
thanks
var content ="<div data-role='collapsible' id='set" + i + "'><h3>Sectionit " + i + "</h3><p>I\'m collapsible number ' + i + '</p></div>";
$('#set').append(content).trigger("create");
$('#set').collapsibleset('refresh');
Instead of
var content ="<div data-role='collapsible' id='set" + i + "'><h3>Sectionit " + i + "</h3><p>I\'m collapsible number ' + i + '</p></div>";
$('#set').append(content).trigger("create");
$('#set').collapsibleset('refresh');
try
$('#set').empty();
var content ="<div data-role='collapsible' id='set" + i + "'><h3>Sectionit " + i + "</h3><p>I\'m collapsible number ' + i + '</p></div>";
$('#set').append(content);
$('#set').collapsibleset().trigger("create");
In this way you only call trigger('create') once after all content has been added.
Here is a DEMO

Categories

Resources