很实用的一个功能,实现的功能就是指定一个高度,一旦图片高度超过这个高度,那图片就自动缩小到一定宽度(因为宽度好控制),高度设置为自动,这样图片就按比例缩放啦,然后点击缩小后的图片就恢复原图,再点击缩小,代码是站长自己写的,可能有点烂。但是对于小站够用了。
Jquery
第一种方法:
- <script type="text/javascript">
- $(document).ready(function(){
- var yheight = 500;//高度条件
- $(".panel img").each(function(){
- var yheights=$(this).height();
- var ywidth=$(this).width();
- if(yheights > yheight) {
- $(this).width(150);
- $(this).css("height","auto");
- var xheight=$(this).height();
- $(this).mouseover(function(){
- $(this).css("cursor","pointer");
- });
- $(this).click(function(){
- if($(this).width()<ywidth){
- $(this).height(yheights);
- $(this).width(ywidth);
- }else
- {
- $(this).width(150);
- $(this).height(xheight);
- }
- });
- }
- });
- });
- </script>
第二种方法:
- jQuery(window).load(function () {
- jQuery(".panel img").each(function () {
- DrawImage(this, 680, 1000);
- });
- });
- function DrawImage(ImgD, FitWidth, FitHeight) {
- var image = new Image();
- image.src = ImgD.src;
- if (image.width > 0 && image.height > 0) {
- if (image.width / image.height >= FitWidth / FitHeight) {
- if (image.width > FitWidth) {
- ImgD.width = FitWidth;
- ImgD.height = (image.height * FitWidth) / image.width;
- } else {
- ImgD.width = image.width;
- ImgD.height = image.height;
- }
- } else {
- if (image.height > FitHeight) {
- ImgD.height = FitHeight;
- ImgD.width = (image.width * FitHeight) / image.height;
- } else {
- ImgD.width = image.width;
- ImgD.height = image.height;
- }
- }
- }
- }
HTML
- <div class="panel">
- <img src="http://img.daimajiayuan.com/uploads/allimg/1308/1-130R6163610.jpg" />
- </div>
零七网部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与零七网进行文章共享合作。