前面要加載一個(gè)jquery庫(kù)。
<script type="text/javascript"> var setNewsImg = function(obj){ $(obj).find('img').each(function(i){ var imgw = $(this).width(); var imgh = $(this).height(); var scale=1; if(imgw>634){ scale = 634/imgw; console.log(scale); $(this).height(scale*imgh); $(this).width(scale*imgw); } }); } </script type="text/javascripy"> 調(diào)用 <script> $(function(){ setNewsImg('.news-img') }) </script>
代碼二:
$(window).bind("load", function() { // 圖片修改大小 $('#imglist img').each(function() { var maxWidth = 120; //初始化最大顯示寬度 var maxHeight = 120; //初始化最大顯示高度 var ratio = 0; var width = $(this).width(); var height = $(this).height(); if(width > maxWidth){ ratio = maxWidth / width; $(this).css("width", maxWidth); $(this).css("height", height * ratio); height = height * ratio; } if(height > maxHeight){ ratio = maxHeight / height; $(this).css("height", maxHeight); $(this).css("width", width * ratio); width = width * ratio; } }); });
代碼三:
<script>$(function(){$(".daima img").each(function(){maxWidth =700;ratio = 0;width = $(this).width();height = $(this).height();if(width > maxWidth){ratio = maxWidth / width;$(this).width(maxWidth);height = height * ratio;$(this).height(parseInt(height));}});});</script>
代碼四: 推薦使用
$('#content').find('img').each(function(){ var img = this; if (img.width > 600) { img.style.width = "600px"; img.style.height = "auto"; //$(img).removeAttr('height'); var aTag = document.createElement('a'); aTag.href = img.src; aTag.target="_blank"; $(aTag).addClass('bPic') .insertAfter(img).append(img) .lightBox(options); } });
這個(gè)content就是文章內(nèi)容部分的id,方便控制制定位置的圖片大小,推薦使用。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注