dragon7088 2017-05-10 07:25
浏览 52

Javascript公式,用于根据原始宽度设置图像宽度,但以百分比表示

This seems basic but i can't wrap my head around it.Honest ain't got no clue but what i need is set the image width in percentage just like in css file but this time around i wonna know the method or technique used to do that.

Example..

//I have tried this but it didn't work
//var image_width_from_server=(the_img_width_from_php);

//image_width_from_server=image_width_from_server/$(parent).width()*90


//Now the result i get from the console does not match the browser set width of the image's p-ercentage width.
#parent{
width:250px;
height:250px;
background:deepskyblue;
border:5px;
}

#parent img{
width:90%;/**Now how can i do this in javaScript..?*/
height:auto;
}


/**Please note that direct css code won't help.
I need a way in javaScript to set the image's width using percentage but also basing on both the image's width size and the parent width size too.
*/
<div id='parent'>
  <img src='https://i.vimeocdn.com/video/584087165_780x439.jpg'/>
</div>

Please note that direct css code won't help. I need a way in javaScript to set the image's width using percentage but also basing on both the image's width size and the parent width size too.

Am really out-of ideas, Any help appreciated.

Thanks

</div>
  • 写回答

3条回答 默认 最新

  • dongqiaogouk86049 2017-05-10 07:34
    关注

    First select your image and after that you can set the width from js.

    $('#parent > img').css('width', '90%');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应