Calculate the width and height of parent div and apply to image accordingly

怎甘沉沦 提交于 2019-12-06 09:13:05
Nope

Using jQuery you can use .height(), .innerHeight() or .outerHeight().

The differences are:

  • height() returns just the height of the element, no borders, no margins and no padding
  • innerHeight() returns the element height and the padding
  • outerHeight() returns the element height, the padding and borders
  • outerHeight(true) returns the element height, the padding, borders and margins

I have more details including output examples using jsFiddle in this post here.

For width you can use width(), innerWidth() or outerWidth().
The same logic applies as with height.

All values are in pixels.

To get the height/width you can use it similar to this:

// The below is an example, you need to add your element references as required.

// Use height(), innerHeight() or outerHeight() as needed.
var newHeight = $("#slider-wrapper").height();

// Use width(), innerWidth() or outerWidth() as needed.
var newWidth = $("#slider-wrapper").width();

To set the height/width you can use it similar to:

// The below is an example, you need to add your element references as required.
var newHeight = $("#slider-wrapper img").height(newHeight);
var newWidth = $("#slider-wrapper img").width(newWidth);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!