How get image size of image loaded by lazy load - javascript

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.

Related

How to write the HTML code for the inline element showing an image for use in webpage?

I'm writing my first code in an interactive/follow-along program through Cengage (MindTap). The program is instructing me to "write the HTML code for the inline element showing the sky image to use in the webpage." I am supposed to create a variable named imgStr that stores this text string:
<img src='sd_skyMap.png' />
Where Map is the value of the mapNum variable (there are 23 files titled sd_sky0, sd_sky1, sd_sky3 and so fourth). It says to use the + operator to combine text strings together and to include single-quote characters within the text strings.
I cannot get the sky images to appear on the webpage to save my life.
I've attempted going through a tutor provided through my university but have still have no luck getting the image to display.
var imgStr = "<img src='sd_sky0 + sd_sky1 + sd_sky2 + sd_sky3 +
sd_sky4 + sd_sky5 + sd_sky6 + sd_sky7 + sd_sky8 + sd_sky9 +
sd_sky10 + sd_sky11 + sd_sky12 + sd_sky13 + sd_sky14 + sd_sky15
+ sd_sky16 + sd_sky17 + sd_sky18 + sd_sky19 + sd_sky20 +
sd_sky21 + sd_sky22 + sd_sky23' + mapNum + '.png' />";
document.getElementById("planisphere").insertAdjacentHTML() = imgStr;
Having inserted the code into jshint.com, it stated one warning and one unused variable.
(Bad assignment.)
document.getElementById("planisphere").insertAdjacentHTML() = imgStr;
and mapNum is an unused variable.
InsertAdjacentHTML takes two strings as parameters.
The first parameter is the position which takes one of four static values.
The second parameter is your HTML string to be inserted.
An example for what you want could be:
document.getElementById("planisphere").insertAdjacentHTML('afterbegin', imgStr);
You were nearly there, just append beforeend using the document.insertAdjacentHTML()
const imgStr = `<img src='sd_sky0 + sd_sky1 + sd_sky2 + sd_sky3 +
sd_sky4 + sd_sky5 + sd_sky6 + sd_sky7 + sd_sky8 + sd_sky9 +
sd_sky10 + sd_sky11 + sd_sky12 + sd_sky13 + sd_sky14 + sd_sky15
+ sd_sky16 + sd_sky17 + sd_sky18 + sd_sky19 + sd_sky20 +
sd_sky21 + sd_sky22 + sd_sky23' + mapNum + '.png' />`;
document.getElementById("planisphere").insertAdjacentHTML('beforeend', imgStr);
<div id = "planisphere">
</div>
There are two problems on your code, the first is you need to run trough the different image files and add each one separately. On the code you provided, all image's names are being combined as one.
The second problem is your use of the insertAdjacentHTML() function. The function expects as arguments the position of the new tag and the tag itself, none is being passed. Check here for a better explanation.
Assuming you have n images that you want to add as n tags, you can try something like this:
// variable to hold the total number of images used
var numberOfImages = 23;
// we loop trough all images, where i will count from 0 to numberOfImages
for (var i = 0; i < numberOfImages; i++) {
// on each step of the loop we add a new img tag with sd_skyi as source
document.getElementById("planisphere")
.insertAdjacentHTML('afterend', "<img src='sd_sky" + i + ".png' />")
}
If you use this exerpt as is, it will add 23 img tags to an element with id planisphere.

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

is loading all background images different from <img> preloading

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.

How to clear the element in a open popup box

Currently I am using an click element to open a popup box to share some content on the social network
The code i used is :
$('#qq').attr("onclick", "window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(shareDes) + "&pics=" + imgStr + "', 'QQshare','toolbar=0,status=0,width=800,height=500');");
Which generate a box , and in that box there is an textarea
<textarea class="view_summary" id="summary" ><%=escHTML(summary)%></textarea>
How to clear the element inside it? I simply add $('.view_summary').empty(); after the create box code, which does not work. Thanks
If the new window has the same domain as the page opening it then try
$('#qq').on("click", function(){
var win = window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(shareDes) + "&pics=" + imgStr + "', 'QQshare','toolbar=0,status=0,width=800,height=500');
win.onload = function(){
$('#summary', win.document).val('');
}
});
Demo: Plunker
i'm not sure on this, but you might want to try $('#summary').empty();

Categories

Resources