Jquery get height of fluid image - javascript

I need to get the height of an image and apply the height to another div. This is easy using
$(window).load(function() {
var img_height = $('#image').height();
$('.some_div').css('height', img_height);
});
however... the image is set to width 100% and changes size when re-sizing the browser window. Is it possible to get the live height of the image?

$(window).resize(function () {
var image_height = $('img').height();
$('div').css({
'height': image_height
});
});
Here's a JSFiddle which uses the Console to report back the image height.

You can use window resize callback
$(window).resize(function(){
var img_height = $('#image').height();
$('.some_div').css('height', img_height);
})

Related

The image doesn't get more wide

I was trying to make a mini image editor with a width and height function. The image should keep getting more wide after button is clicked. I tried debugging with printing the width in the console and it did but the image didn't get wider. I also tried many other variations and all of them didn't work.
so, here is my code:
function go1(){
setInterval( function() {document.getElementById("testimage").style.width + 10}, 250)
}
Your first problem is that while you are calculating a new width, you are aren't setting the new width in your code.
The second problem is that IMG tags don't use the width style. Instead, they have a width attribute
function go1(){
setInterval( function() {
var im = document.getElementById("testimage");
var newWidth = (im.getAttribute("width") ? im.getAttribute("width") : 0) + 10;
im.setAttribute("width", newWidth);
}, 250)
}

Justify images inside div using jquery

I have a centered div that takes 80% of the width of window, inside that div have a gallery images .container img {min-width:200px;} using jquery I want to make the images justified depending on the width of the .container, the code that worked for me is this :
$(window).on("resize", function(){
// get the width of the container
var albumContentWidth = $(".container").width();
// as we have in our css the min-width of image is 200
// see how many image can fit in each row
var imagePerRow = parseInt(albumContentWidth / 200);
// set the width of image using calc
$(".container img").css("width", "calc(100% / #{imagePerRow})");
});
The code above works fine, but for some reasons.. I don't want to use calc() instead I want to calculate this using pixels, so what I did :
$(window).on("resize", function(){
// get the width of the container
var albumContentWidth = $(".container").width();
// as we have in our css the min-width of image is 200
// see how many image can fit in each row
var imagePerRow = parseInt(albumContentWidth / 200);
// instead of using calc() divide the width of container by the possible number of images per row
var imageWidth = (albumContentWidth / imagePerRow).toFixed(2);
// set the width of image using calc
$(".container img").css("width", imageWidth);
});
However this is not working and the images don't get justified correctly when I resize !!

adjust responsive size of iframe with jquery

Hello I'm trying to adjust the size of youtube videos in my web. I want them to be responsive that I'm using jquery to do that. I have done it successfully but the thing is I want the height of youtube video to be decreased. Right now, it's too big. When I try to do it, the responsiveness feature is removed. Can you check how I can decrease the size of the video and keep the responsiveness?
<script>
function update_iframe_size(){
var parent_id = $("iframe").parent().attr("id");
if (parent_id == "main_video") {
var parent_class = $("iframe").parent().attr("class");
var parent_width = $("iframe").parent().width();
console.log(parent_class);
var width = $("iframe").css("width"); // $("iframe").width();
var height = $("iframe").css("height");
var ratio = parseInt(height)/parseInt(width);
var new_height = parseInt(parent_width) * ratio
$("iframe").css("width", parent_width);
$("iframe").css("height", new_height);
}
}
update_iframe_size()
$(window).bind("resize", function(){
// alert("reized");
update_iframe_size();
});
</script>
I tried to decrease the height that I did $("iframe").css("height", new_height*0.7);
but then the height is set to the one I want. However responsiveness gets messed up.
Refer following link: iFrame always take height and width 100% depends on parent div/element which is position relative.
http://jsfiddle.net/masau/7wrhm/

calculate height and apply to a div with jquery

I have a banner image that is only visible when i set a fixed height, i want to make it responsive so the only way i thought of doing that is calculating the height of the image with the width of the window.
the function to calculate the height is: windowWidth/1.845
how do i apply that function to a div css with jquery/javascript?
i tried this function but it didn't work
$(document).ready(function(){
var originalWidth = $( window ).width();
var newHeight = originalWidth/+(1.845)
;
function resize() {
$("#ei-slider").css("height", newHeight);
}
$(".target").each(resize);
$(document).resize(function(){
$("ei-slider").each(resize);
});
});
Try adding () when you call your function resize
$(".target").each(resize());
$(document).resize(function(){
$(".ei-slider").each(resize());
});
Also, you miss the point before ei-slider
And I highly recommend to you to use backstretch jquery plugin.
http://srobbin.com/jquery-plugins/backstretch/
Why is the image only visible when you set a fixed height? An image would be "responsive" automatically if you were to do this in your CSS.
img {
max-width: 100%;
width: 100%;
}
However if you need to do other things in the resize you probably want to listen for resizes on window and not document.
$(document).ready(function () {
var $window = $(window);
var originalWidth = $window.width();
var newHeight = originalWidth / 1.845;
function resize() {
// This will be .target or .ei-slider based on where it's called.
$(this).css("height", newHeight);
};
$(".target").each(resize);
$window.on('resize', function(){
$(".ei-slider").each(resize);
});
});

Get browser width and height after user resizes the window

Is there any way to get the browser width and height after a user has resized the window. For example if the window is 1920 by 1080 and the user changes the window to 500 by 500 is there any way to get those two new values in JavaScript or jquery?
Pure Javascript answer:
var onresize = function() {
//your code here
//this is just an example
width = document.body.clientWidth;
height = document.body.clientHeight;
}
window.addEventListener("resize", onresize);
This works fine on chrome. However, it works only on chrome. A slightly more cross-browser example is using the event target properties "outerWidth" and "outerHeight", since in this case the event "target" is the window itself. The code would be like this
var onresize = function(e) {
//note i need to pass the event as an argument to the function
width = e.target.outerWidth;
height = e.target.outerHeight;
}
window.addEventListener("resize", onresize);
This works fine in firefox and chrome
Hope it helps :)
Edit: Tested in ie9 and this worked too :)
If you need to know these values to do layout adjustments, I bet you plan on listening to those values. I recommended using the Window.matchmedia() API for that purpose instead.
It is much more performant and is basically the JS equivalent of CSS media queries.
Very quick example of use:
if (window.matchMedia("(max-width: 500px)").matches) {
/* the viewport is less than or exactly 500 pixels wide */
} else {
/* the viewport is more than 500 pixels wide */
}
You can also setup a listener that'll get called every time the state of the matches property changes.
See MDN for description and example of using a listener.
It's possible by listening to resize event.
$(window).resize(function() {
var width = $(window).width();
var height = $(window).height();
})
You can use the JQuery resize() function. Also make sure you add the same resize logic to reload event. If user reloads in the sized window your logic won't work.
$(window).resize(function() {
$windowWidth = $(window).width();
$windowHeight = $(window).height();
});
$(document).ready(function() {
//same logic that you use in the resize...
});
Practically, I use this and it helps me a lot:
var TO = false;
var resizeEvent = 'onorientationchange' in window ? 'orientationchange' : 'resize';
$(window).bind(resizeEvent, function() {
TO && clearTimeout(TO);
TO = setTimeout(resizeBody, 200);
});
function resizeBody(){
var height = window.innerHeight || $(window).height();
var width = window.innerWidth || $(window).width();
alert(height);
alert(width);
}
You can use the resize event, along with the height() and width() properties
$(window).resize(function(){
var height = $(window).height();
var width = $(window).width();
});
See some more examples here
Use jQuery resize method to listen window size change . inside callback you can get height and width.
$(window).resize(function(){
var width = $(window).width();
var height = $(window).height();
});
Simplest way to get real width and height of an element after window resize as the follow:
<div id="myContainer">
<!--Some Tages ... -->
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function () {
$(window).resize(function () {
//The below two lines of codes more Important to clear the previous settings to get the current measure of width and height
$('#myContainer').css('height', 'unset');
$('#myContainer').css('width', 'unset');
var element = $('#myContainer');
var height = element.height();
var width = element.width();
//Below two lines will includes padding but not border
var innerHeight = element.innerHeight();
var innerWidth = element.innerWidth();
//Below two lines will includes padding, border but no margin
var outerHeight = element.outerHeight();
var outerWidth = element.outerWidth();
//Below two lines will includes padding, border and margin
var outerHeight = element.outerHeight(true);
var outerWidth = element.outerWidth(true);
});
});
</script>
You can use the event object to get the height and width, I use destructuring assignment and the target points to window:
const handleGetDim = ({ target }) => ({
width: target.innerWidth,
height: target.innerHeight,
});
window.addEventListener('resize', handleGetDim);

Categories