jquery实现图片按比例缩放示例
2019/6/29 22:36:22
本文主要是介绍jquery实现图片按比例缩放示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<html> <title></title> <head></head> <style> .thumbnail{overflow:hidden;width:400px;height:240px;} </style> <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script> <script language="javascript"> $(function(){ /* 图片不完全按比例自动缩小 by zwwooooo */ $('#content div.thumbnail img').each(function(){ var x = 400; //填入目标图片宽度 var y = 240; //填入目标图片高度 var w=$(this).width(), h=$(this).height();//获取图片宽度、高度 if (w > x) { //图片宽度大于目标宽度时 var w_original=w, h_original=h; h = h * (x / w); //根据目标宽度按比例算出高度 w = x; //宽度等于预定宽度 if (h < y) { //如果按比例缩小后的高度小于预定高度时 w = w_original * (y / h_original); //按目标高度重新计算宽度 h = y; //高度等于预定高度 } } $(this).attr({width:w,height:h}); }); }); </script> <body> <div id="content"> <div id="thumbnail" class="thumbnail" > <img id="web" src="./midle.png"/> </div> </div> </body> </html>
这篇关于jquery实现图片按比例缩放示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-06jquery对css样式(jquery中的css方法)-icode9专业技术文章分享
- 2023-05-27JQuery的认识和安装
- 2023-01-06JQuery应用技巧:如何定义 HTML 模板并使用 JQuery 进行加载-icode9专业技术文章分享
- 2022-09-29复习-jQuery
- 2022-09-04Python3项目初始化10-->前端基础jquery、ajax,sweetalert--更新用户改造
- 2022-08-30day 27 jquery
- 2022-08-29jQuery筛选器,bootstrap
- 2022-08-20JQuery事件绑定
- 2022-08-20JQuery案例
- 2022-08-07关于jQuery的学习