本文目錄一覽:
- 1、怎麼用js實現圖片的縮小?
- 2、jS控制圖片的放大和縮小?
- 3、求一個簡單的點擊圖片放大縮小的JS代碼
- 4、如何利用JS或者CSS樣式來自動調整圖片大小
- 5、js實現圖片滾輪、按鈕縮放大小,圖片旋轉,圖片拖拽
怎麼用js實現圖片的縮小?
一般來說,實現圖片的放大縮小功能都用到了比較大的封裝插件,特別是以jQuery插件居多,而實際上單純實現對原圖本身的放大縮小,用簡單幾行原生JS代碼就可以做到。在今天分享的這個實例中,點擊放大按鈕不松滑鼠,圖片會不斷的逐漸放大,當然也可以點一下放大一點,點擊縮小按鈕則反之,有需要的朋友可以考慮收藏備用哦
以下為全部代碼:
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /
titlejavascript控制圖片縮小或者放大/title
/head
body
script type=”text/javascript”
var oTime;
function changeSize(id,action){
var obj=document.getElementById(id);
obj.style.zoom=parseInt(obj.style.zoom)+(action==’+’?+10:-10)+’%’;
oTime=window.setTimeout(‘changeSize(\”+id+’\’,\”+action+’\’)’,100);
}
document.onmouseup=function(){
window.clearTimeout(oTime);
}
/script
div style=”height:350px; overflow: auto;”
img id=”headImg” src=”
button onmousedown=”changeSize(‘headImg’,’+’);” onmouseup=”window.clearTimeout(oTime);”放大/button
button onmousedown=”changeSize(‘headImg’,’-‘);” onmouseup=”window.clearTimeout(oTime);”縮小/button
/body
/html
jS控制圖片的放大和縮小?
圖片按比例縮放
function DrawImage(Img,WIDTH,HEIGHT){
var image=new Image();
image.src=Img.src;
width=WIDTH;//預先設置的所期望的寬的值
height=HEIGHT;//預先設置的所期望的高的值
if(image.widthwidth||image.heightheight){//現有圖片只有寬或高超了預設值就進行js控制
w=image.width/width;
h=image.height/height;
if(wh){//比值比較大==寬比高大
//定下寬度為width的寬度
Img.width=width;
//以下為計算高度
Img.height=image.height/w;
}else{//高比寬大
//定下寬度為height高度
img.height=height;
//以下為計算高度
Img.width=image.width/h;
}
}
}
img src=”xxxx” onload=javascript:DrawImage(this,290,215);
求一個簡單的點擊圖片放大縮小的JS代碼
1、準備好需要用到的圖標。
2、新建html文檔。
3、書寫hmtl代碼。div id=allbox div id=boxhome img style=”WIDTH: 107px; BOTTOM: 5px; HEIGHT: 176px; LEFT: 10px” id=imgSmallLeft class=imgBorder onClick=”clearInterval(autoplay);moveD(‘l’);” 。
4、書寫並添加js代碼。script src=”js/ntes_jslib_1.x.js”/scriptscript src=”js/zzsc.js”/script。
5、代碼整體結構。
6、查看效果。
如何利用JS或者CSS樣式來自動調整圖片大小
js版和css版自動按比例調整圖片大小方法,分別如下:
titlejavascript圖片大小處理函數/title
script language=Javascript
var proMaxHeight = 150;
var proMaxWidth = 110;
function proDownImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width0 image.height0){
var rate = (proMaxWidth/image.width proMaxHeight/image.height)?proMaxWidth/image.width:proMaxHeight/image.height;
if(rate = 1){
ImgD.width = image.width*rate;
ImgD.height =image.height*rate;
}
else {
ImgD.width = image.width;
ImgD.height =image.height;
}
}
}
/script
/head
body
img src=”999.jpg” border=0 width=”150″ height=”110″ onload=proDownImage(this); /
img src=”room.jpg” border=0 width=”150″ height=”110″ onload=proDownImage(this); /
/body
純css的防止圖片撐破頁面的代碼,圖片會自動按比例縮小:
style type=”text/css”
.content-width {MARGIN: auto;WIDTH: 600px;}
.content-width img{MAX-WIDTH: 100%!important;HEIGHT: auto!important;width:expression(this.width 600 ? “600px” : this.width)!important;}
/style
div class=”content-width”
pimg src=”/down/js/images/12567247980.jpg”//p
pimg src=”/down/js/images/12567247981.jpg”//p
/div
js實現圖片滾輪、按鈕縮放大小,圖片旋轉,圖片拖拽
div class=”upload-img-box” ref=”moveWrap”
div class=”show-box” ref=”rotate” @mousedown=”moveImg” @mousewheel.prevent=”rollImg($event,)”
img :src=”singleList.fileImgUrl” class=”uploadimg” ref=”img” :style=”{transform:’scale(‘+multiples/100+’) rotate(‘+rotate +’deg)’}”/
/div
div class=”img-plus” @click=”toBIgChange()”span class=”tip”放大/span/div
div class=”img-minus” @click=”toSmallChange()”span class=”tip”縮小/span/div
div class=”img-rotate” @click=”toRotate()”span class=”tip”旋轉/span/div
/div
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257364.html