I want to change image height only when its src is not empty
with JQuery or JavaScript , i have an image that have fixed height and in chrome they always keep blank place for it even when there is no source for it i want show this height only when image have source
jQuery is not necessary, just use css to define a zero height for images with an empty src attribute:
img[src=""] {
height: 0
}
If you want to use javascript instead of CSS, you can try calling this function whenever you want to check if the image has a source / display the image.
Set an ID for your image and then:
function check()
{
if (document.getElementById("yourimgidhere").src == "")
{
document.getElementById("yourimgidhere").style.display = "none";
}
else
{
document.getElementById("yourimgidhere").style.display = "inline";
}
}
The CSS method is far more effective and is the proper way of doing it as other people have answered, but this should work too.
You can use
img[src=""] {
display: none;
}
You can change the height or not display it..
Related
I've made a quiz which has 10 questions, and stores your points in a value called total. The total points you are able to get is 20, so when total > 10, I want the background to turn red.
I have already set a background using CSS on my website here:
<style type="text/css">
body{
background-image: url("twins.jpg");
}
However I can't seem to get my condition to work properly. I've tried:
if (total > 10) {
document.body.style.backgroundColor = "red";
}
I've tried:
if (total > 10) {
document.body.style.backgroundColor = "#AA0000";
}
And:
if (total > 10) {
document.getElementById('body').style.backgroundImage = "url(ashishot.jpg)";
}
But nothing seems to work. Maybe I'm placing my if statement in the wrong place, or maybe I'm trying to set the background incorrectly, I just want to know if this is the correct way to change a background image from CSS into JavaScript.
Yes this is the correct way to change background color using JS.
document.body.style.backgroundColor = "#AA0000";
Please try:
Placing alert() inside the IF block to see if block is executing
See if something is overlayed(z-indexed) on body
The javascript instruction is correct
document.body.style.backgroundColor = "red";
Your error may be the if condition, check if total its work properly usign the console or an alert to check it:
console.log(total);
or
alert(total);
You have more than one issue
Use directly the body property
document.body.style.backgroundImage
You need to quote the whole assignment and you need inner quotes for the image
'url("ashishot.jpg")';
Working example:
function setBackground() {
document.body.style.backgroundImage = 'url("http://lorempixel.com/300/200/")';
}
setTimeout(setBackground, 2000);
body {
background-image: url("http://lorempixel.com/400/200/");
}
if (total > 10) {
document.body.style.backgroundImage = "url(ashishot.jpg)";
}
body is not an id. also, setting background-color will not work because browser treat background-image first. To display background-color instead of background-image, simply delete the image. Setting classes is more flexible.
document.body.classList.add("total");
body {
background-image: url("http://www.studiocity-macau.com/uploads/images/SC/Entertainment/Batman/batman_share.jpg");
}
body.total {
background-image: none;
background-color: red;
}
Be aware that your backgound-image will be on top of the color you are setting in the if block. If the image covers the background entirely you will not notice the effect of changing the background color.
Both document.body.style.backgroundColor = "#AA0000" and document.body.style.backgroundColor = "red" are valid (although "red" == "#FF0000").
Make sure the if statement runs every time total increases.
I'm using Hertzen's html2canvas.js, and tried to adjust the example code so that it targets a specific div instead of the entire body of a document:
html2canvas(document.body, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
}
});
A sample of what I'm trying to accomplish is here: https://jsfiddle.net/ununkg3a/
On clicking the "Save PNG" button, I want to append an image of the jQuery generated squares that I'm targeting in a specific div. In the code, it appears that it's appending something, but it doesn't have a height. When I try to set a height with the output, it still doesn't work as expected.
Is it not possible to accomplish something like this? Whenever I change document.body to another element, the screenshot doesn't render anymore, although it does render when I change it back to document.body. Someone told me that I'd have to crop the image with js, but that seems a little hacky.
It can: it's the first attribute.
(fiddle)
Example:
html2canvas(document.getElementById('test')).then(function(canvas) {
document.body.appendChild(canvas);
});
In your example, canvas2html can't find out the height of your div. Because of that it falls back to 0px height, so you won't see anything.
Give width to the #art then you can count the height manually and use that.
Mathematic example:
art_square_width = 10px
art_square_height = 10px
art_square_amount = 500
art_width = 250px
art_height = art_width / (art_width / art_square_width) * art_square_height = 200px
(fiddle)
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.
This question already has answers here:
Get the real width and height of an image with JavaScript? (in Safari/Chrome)
(30 answers)
Closed 9 years ago.
I am scaling images to fit within a div and I want to center them with letterboxes by using positions, but when I try to get the width of the element with .width() immediately after setting the height, .width() returns 0.
For example:
$(image).css("height", "100%");
console.log($(image).width());
This echoes 0 to the console, but if I call $(image).width() from the console sometime later, the correct value is given. How can I get the width of the <img> element immediately after I change it's height?
Edit:
So my code goes something like this:
$(window).load(function(){
cSCMH();
});
function cSCMH()
{
//Some other code
for(var i = 0; i < $("sc mh im img").length; i++)
{
var image = $("sc mh im img")[i];
//More code
$(image).css("height", "100%");
console.log($(image).width());
}
}
The console still receives 0
Edit 2:
Okay so I think I know what the root of the problem is. The images don't appear to load into the page until they are made visible on the screen (I have them in a tab div where the display is set to none onload).
Is there any way to force the images to load even though they are not visible?
I have confirmed this is the issue. The images are not loaded until the div's display is set to block. How can I make the images load in onpageload instead?
Try using the image's load event
function cSCMH() {
$("sc mh im img").load(function () {
var $img = $(this);
$img.css("height", "100%");
console.log($img.width());
}).filter(function () {
return this.complete;
}).trigger('load');
}
Since height() does not provide callback function, You can use animate() here:
$(image).animate({"height": '100%'}, function() {
console.log($(image).width());
})
Also, to make sure all the images are loaded properly, you can wrap your code inside $(window).load(function() { ... });
$(window).load(function() {
$(image).animate({"height": '100%'}, function() {
console.log($(image).width());
})
});
You need to wait for dom change.
$(image).css("height", "100%");
setTimeout(function() {
console.log($(image).width());
}, 0);
setting height in percentage wouldn't set it when you haven't set it's parent's div fixed height. So you can also try by using height 100% to your body like this:
html,body{height: 100%;}
In an HTML document there are few div tags with ids DIV1, DIV2
In DIV1 there are 2 images. in DIV2 there is only a single image. Using javascript I want to change the size of images which are in DIV1 to 100px. How can I specify images which are in that particular div tag?
Assuming that those are ids:
#div1 img {
width: 100px;
]
Or, if you prefer (for some reason, though you should use CSS for this), you can use JavaScript:
var div1Images document.getElementById('div1').getElementsByTagName('img');
for (var i = 0, len = div1Images; i < len; i++){
div1Images[i].style.width = '100px';
}
The reason I set only one dimension (width) is that this way the image's height will be automatically adjusted to fit maintain the natural aspect-ratio.
document.querySelectorAll('.div1 img')
This will give you a NodeList object of all of the img elements in <div class=div>` (obviously change as needed). You can iterate over it like a normal array and update the element widths to whatever you want .. even 100px!
If you want to use jQuery its should be as simple as:
$(document).ready(function () {
//select images and adjust height & width however you want, or use just width to scale proportionately
$('#div1 img').width('100px');
$('#div1 img').height('100px');
});
Here's a jfiddle example: http://jsfiddle.net/t9pUL/4/
However, I agree with David Thomas that you should probably use css for this unless you have a real reason to use javascript.