1. 原生js,實現圖片可拖拽,並且移動四個角和四條變能夠自由變換圖片大小
見證奇跡的時刻到了:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<style>
*{
margin:0;
padding:0
}
.box{
position:absolute;
top:100px;
left:100px;
}
/*四邊*/
.box.t,
.box.b,
.box.l,
.box.r{
position:absolute;
z-index:1;
background:#666;
}
.box.l,
.box.r{
width:10px;
height:100%;
cursor:col-resize;
}
.box.t,
.box.b{
width:100%;
height:10px;
cursor:row-resize;
}
.box.t{
top:0;
}
.box.b{
bottom:0;
}
.box.l{
left:0;
}
.box.r{
right:0;
}
/*四角*/
.box.tl,
.box.bl,
.box.br,
.box.tr{
width:20px;
height:20px;
position:absolute;
background:#CCC;
z-index:2;
cursor:nwse-resize
}
.box.tl,
.box.bl{
left:-5px;
}
.box.tr,
.box.br{
right:-5px;
}
.box.br,
.box.bl{
bottom:-5px;
}
.box.tl,
.box.tr{
top:-5px;
}
.box.tr,
.box.bl{
cursor:nesw-resize;
}
/*圖片*/
img{
width:100%;
height:100%;
}
</style>
<script>
window.onload=function(){
varoDiv=document.getElementsByTagName('div')[0];
oDiv.style.width='500px';
varaSpan=oDiv.getElementsByTagName('span');
for(vari=0;i<aSpan.length;i++){
dragFn(aSpan[i]);
}
functiondragFn(obj){
obj.onmousedown=function(ev){
varoEv=ev||event;
varoldWidth=oDiv.offsetWidth;
varoldHeight=oDiv.offsetHeight;
varoldX=oEv.clientX;
varoldY=oEv.clientY;
varoldLeft=oDiv.offsetLeft;
varoldTop=oDiv.offsetTop;
document.onmousemove=function(ev){
varoEv=ev||event;
if(obj.className=='tl'){
oDiv.style.width=oldWidth-(oEv.clientX-oldX)+'px';
oDiv.style.height=oldHeight-(oEv.clientY-oldY)+'px';
oDiv.style.left=oldLeft+(oEv.clientX-oldX)+'px';
oDiv.style.top=oldTop+(oEv.clientY-oldY)+'px';
}
elseif(obj.className=='bl'){
oDiv.style.width=oldWidth-(oEv.clientX-oldX)+'px';
oDiv.style.height=oldHeight+(oEv.clientY-oldY)+'px';
oDiv.style.left=oldLeft+(oEv.clientX-oldX)+'px';
oDiv.style.bottom=oldTop+(oEv.clientY+oldY)+'px';
}
elseif(obj.className=='tr'){
oDiv.style.width=oldWidth+(oEv.clientX-oldX)+'px';
oDiv.style.height=oldHeight-(oEv.clientY-oldY)+'px';
oDiv.style.right=oldLeft-(oEv.clientX-oldX)+'px';
oDiv.style.top=oldTop+(oEv.clientY-oldY)+'px';
}
elseif(obj.className=='br'){
oDiv.style.width=oldWidth+(oEv.clientX-oldX)+'px';
oDiv.style.height=oldHeight+(oEv.clientY-oldY)+'px';
oDiv.style.right=oldLeft-(oEv.clientX-oldX)+'px';
oDiv.style.bottom=oldTop+(oEv.clientY+oldY)+'px';
}
elseif(obj.className=='t'){
oDiv.style.height=oldHeight-(oEv.clientY-oldY)+'px';
oDiv.style.top=oldTop+(oEv.clientY-oldY)+'px';
}
elseif(obj.className=='b'){
oDiv.style.height=oldHeight+(oEv.clientY-oldY)+'px';
oDiv.style.bottom=oldTop-(oEv.clientY+oldY)+'px';
}
elseif(obj.className=='l'){
oDiv.style.height=oldHeight+'px';
oDiv.style.width=oldWidth-(oEv.clientX-oldX)+'px';
oDiv.style.left=oldLeft+(oEv.clientX-oldX)+'px';
}
elseif(obj.className=='r'){
oDiv.style.height=oldHeight+'px';
oDiv.style.width=oldWidth+(oEv.clientX-oldX)+'px';
oDiv.style.right=oldLeft-(oEv.clientX-oldX)+'px';
}
};
document.onmouseup=function(){
document.onmousemove=null;
};
returnfalse;
};
}
};
</script>
</head>
<body>
<divclass="box">
<spanclass="r"></span>
<spanclass="l"></span>
<spanclass="t"></span>
<spanclass="b"></span>
<spanclass="br"></span>
<spanclass="bl"></span>
<spanclass="tr"></span>
<spanclass="tl"></span>
<imgsrc="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/730-350-0.jpg">
</div>
</body>
</html>
2. js中如何拖動DIV中的圖片
代碼放出來看抄看
是不是冒襲泡和捕獲的問題
試試簡單的,看看點擊圖片時 div上的onclick事件有沒有被觸發
// 看起來是使用 jquery ,代碼貼完整些吧,
this.mousedown(function(){
中this是指向什麼對象都沒看出來
//加一個 return false
//另外 event.offsetX 並不兼容 firefox
$(document).mousemove(function(){
if(movestart){
$("#id1").css("left",event.clientX-x1+"px");
$("#id1").css("top",event.clientY-y1+"px");
return false;
}
})
3. js如何點擊左右按鈕切換圖片
這樣:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>動態切換圖片</title>
</head>
<style>
ul{
padding:0;margin:0;
}
li{
list-style: none;
}
#pic{
position: relative;
width: 400px;
height: 400px;
background-color:red;
margin:100px auto;
background:url('image/1.jpg') no-repeat center;
}
#pic img{
width: 400px;
height: 400px;
}
#pic ul{
width: 50px;
position: absolute;
top: 0;
right: -70px;
}
li{
width: 40px;
height: 40px;
margin-bottom:10px;
background-color: pink;
float: left;
}
#pic span{
position: absolute;
bottom: 10px;
left: 0;
}
#pic p,#pic span{
width: 400px;
height: 20px;
}
#pic p{
position: absolute;
top: 10px;
left: 0;
}
.active{
background-color: red;
}
</style>
<body>
<div id="pic">
<img src="" alt="">
<p>qwrwe</p>
<span>werwer</span>
<ul>
</ul>
</div>
<script>
window.onload=function(){
//存放舊li
var oldLi=null;
var num=0;
var oPic = document.getElementById('pic');
var oImg = oPic.getElementsByTagName('img')[0];
var oUL = oPic.getElementsByTagName('ul')[0];
var oSpan= oPic.getElementsByTagName('span')[0];
var oP = oPic.getElementsByTagName('p')[0];
var oLi= oUL.getElementsByTagName('li');
var arr=['image/1.jpg','image/2.jpg','image/3.jpg','image/4.jpg'];
var aText = ['圖片1','圖片2','圖片3','圖片4'];
for(var i=0;i<arr.length;i++){
//動態添加元素
oUL.innerHTML+='<li></li>';
}
// 舊li就等於當前的
oldLi=oLi[num];
// 初始化
oImg.src=arr[num];
oP.innerHTML=num+1+'/'+arr.length;
oSpan.innerHTML=aText[num];
oLi[num].className='active';
for(var i=0;i<arr.length;i++){
// 給元素自定義屬性
//
oLi[i].index=i;
oLi[i].onclick=function(){
// 當元素被點擊時圖片文字信息都一起變化
oImg.src=arr[this.index];
oP.innerHTML=1+this.index+'/'+arr.length;
oSpan.innerHTML=aText[this.index];
// 清空上一個 當前添加
oldLi.className='';
//將上一個給當前
oldLi=this;
this.className='active';
}
}
}
</script>
</body>
</html>
注意事項
1、可以通過JS刪除和添加hidden屬性,改用style.display="none"和style.display="inline"來實現隱藏和顯示。
2、button屬性,主要的問題時button樣式的問題,如何才能做一個好看的button,通過查找找到了設置button相關的值。
border:none; 設置按鈕無邊框
outline:none;消除按鈕點擊後出現的表示被點擊的邊框
background:url(...)按鈕背景圖片
text-shadow: 0 1px 1px rgba(0,0,0,.3);文字陰影
box-shadow: 0 5px 7px rgba(0,0,0,.2);按鈕陰影
border-radius:15px;按鈕邊框圓角
4. js製作滑鼠移動到圖片上的動畫
//html
onmouseover=mouseOver()換成onmouseover=mouseOver(this)
onmouseout=mouseOut()換成onmouseout=mouseOut(this)
//js更改透明度即可,不需要換圖片
functionmouseOver(obj){
obj.style.opacity=0.5;
}
functionmouseOut(obj){
obj.style.opacity=1;
}
5. JS控制圖片向左向右移動的代碼
這段代碼可以: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>圖片</title> <script language="javascript"> <!-- //圖片滾動列表 mengjia 070927 var Speed_1 = 10; //速度(毫秒) var Space_1 = 20; //每次移動(px) var PageWidth_1 = 116 * 3; //翻頁寬度 var interval_1 = 7000; //翻頁間隔 var fill_1 = 0; //整體移位 var MoveLock_1 = false; var MoveTimeObj_1; var MoveWay_1="right"; var Comp_1 = 0; var AutoPlayObj_1=null; function GetObj(objName){if(document.getElementById){return eval('document.getElementById("'+objName+'")')}else{return eval('document.all.'+objName)}} function AutoPlay_1(){clearInterval(AutoPlayObj_1);AutoPlayObj_1=setInterval('ISL_GoDown_1();ISL_StopDown_1();',interval_1)} function ISL_GoUp_1(){if(MoveLock_1)return;clearInterval(AutoPlayObj_1);MoveLock_1=true;MoveWay_1="left";MoveTimeObj_1=setInterval('ISL_ScrUp_1();',Speed_1);} function ISL_StopUp_1(){if(MoveWay_1 == "right"){return};clearInterval(MoveTimeObj_1);if((GetObj('ISL_Cont_1').scrollLeft-fill_1)%PageWidth_1!=0){Comp_1=fill_1-(GetObj('ISL_Cont_1').scrollLeft%PageWidth_1);CompScr_1()}else{MoveLock_1=false} AutoPlay_1()} function ISL_ScrUp_1(){if(GetObj('ISL_Cont_1').scrollLeft<=0){GetObj('ISL_Cont_1').scrollLeft=GetObj('ISL_Cont_1').scrollLeft+GetObj('List1_1').offsetWidth} GetObj('ISL_Cont_1').scrollLeft-=Space_1} function ISL_GoDown_1(){clearInterval(MoveTimeObj_1);if(MoveLock_1)return;clearInterval(AutoPlayObj_1);MoveLock_1=true;MoveWay_1="right";ISL_ScrDown_1();MoveTimeObj_1=setInterval('ISL_ScrDown_1()',Speed_1)} function ISL_StopDown_1(){if(MoveWay_1 == "left"){return};clearInterval(MoveTimeObj_1);if(GetObj('ISL_Cont_1').scrollLeft%PageWidth_1-(fill_1>=0?fill_1:fill_1+1)!=0){Comp_1=PageWidth_1-GetObj('ISL_Cont_1').scrollLeft%PageWidth_1+fill_1;CompScr_1()}else{MoveLock_1=false} AutoPlay_1()} function ISL_ScrDown_1(){if(GetObj('ISL_Cont_1').scrollLeft>=GetObj('List1_1').scrollWidth){GetObj('ISL_Cont_1').scrollLeft=GetObj('ISL_Cont_1').scrollLeft-GetObj('List1_1').scrollWidth} GetObj('ISL_Cont_1').scrollLeft+=Space_1} function CompScr_1(){if(Comp_1==0){MoveLock_1=false;return} var num,TempSpeed=Speed_1,TempSpace=Space_1;if(Math.abs(Comp_1)<PageWidth_1/2){TempSpace=Math.round(Math.abs(Comp_1/Space_1));if(TempSpace<1){TempSpace=1}} if(Comp_1<0){if(Comp_1<-TempSpace){Comp_1+=TempSpace;num=TempSpace}else{num=-Comp_1;Comp_1=0} GetObj('ISL_Cont_1').scrollLeft-=num;setTimeout('CompScr_1()',TempSpeed)}else{if(Comp_1>TempSpace){Comp_1-=TempSpace;num=TempSpace}else{num=Comp_1;Comp_1=0} GetObj('ISL_Cont_1').scrollLeft+=num;setTimeout('CompScr_1()',TempSpeed)}} function picrun_ini(){ GetObj("List2_1").innerHTML=GetObj("List1_1").innerHTML; GetObj('ISL_Cont_1').scrollLeft=fill_1>=0?fill_1:GetObj('List1_1').scrollWidth-Math.abs(fill_1); GetObj("ISL_Cont_1").onmouseover=function(){clearInterval(AutoPlayObj_1)} GetObj("ISL_Cont_1").onmouseout=function(){AutoPlay_1()} AutoPlay_1(); } //產品展示滾動圖片結束 //--> </script> <style type="text/css"> <!-- BODY { BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 0px; MARGIN: 0px; FONT-FAMILY: 宋體; BACKGROUND-COLOR: #fff; BORDER-RIGHT-WIDTH: 0px } .blk_18 { BORDER-RIGHT: #e3e3e3 1px solid; BORDER-TOP: #e3e3e3 1px solid; MARGIN-TOP: 8px; FONT-SIZE: 12px; BACKGROUND: #f3f3f3; OVERFLOW: hidden; BORDER-LEFT: #e3e3e3 1px solid; WIDTH: 390px; BORDER-BOTTOM: #e3e3e3 1px solid; ZOOM: 1 } .blk_18 .pcont { FLOAT: left; OVERFLOW: hidden; WIDTH: 350px } .blk_18 .ScrCont { WIDTH: 32766px; ZOOM: 1 } .blk_18 #List1_1 { FLOAT: left } .blk_18 #List2_1 { FLOAT: left } .blk_18 .LeftBotton { BACKGROUND: url( http://img.china.alibaba.com/images/unsort/mole/picr_1.gif ) no-repeat; FLOAT: left; MARGIN: 10px 1px; WIDTH: 15px; HEIGHT: 72px } .blk_18 .RightBotton { BACKGROUND: url( http://img.china.alibaba.com/images/unsort/mole/picr_1.gif ) no-repeat; FLOAT: left; MARGIN: 10px 1px; WIDTH: 15px; HEIGHT: 72px } .blk_18 .LeftBotton { BACKGROUND-POSITION: 0px 0px; MARGIN-LEFT: 5px } .blk_18 .RightBotton { BACKGROUND-POSITION: 0px -100px; MARGIN-LEFT: -1px } .blk_18 .LeftBotton:hover { BACKGROUND-POSITION: -20px 0px } .blk_18 .RightBotton:hover { BACKGROUND-POSITION: -20px -100px } .blk_18 .pl IMG { DISPLAY: block; MARGIN: 6px auto 1px; CURSOR: pointer; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none } .blk_18 .pl { BORDER-RIGHT: #f3f3f3 1px solid; BORDER-TOP: #f3f3f3 1px solid; FLOAT: left; BORDER-LEFT: #f3f3f3 1px solid; WIDTH: 114px; LINE-HEIGHT: 24px; BORDER-BOTTOM: #f3f3f3 1px solid; TEXT-ALIGN: center; TEXT-DECORATION: underline } .blk_18 A.pl:hover { BORDER-RIGHT: #ff9900 1px solid; BORDER-TOP: #ff9900 1px solid; BACKGROUND: #fff; BORDER-LEFT: #ff9900 1px solid; COLOR: #ff9900; BORDER-BOTTOM: #ff9900 1px solid } .commu_cont3 { MARGIN: 9px 7px 7px; LINE-HEIGHT: 150% } .commu_cont3 UL { WIDTH: 188px } --> </style> </head> <body> <!-- picrotate_left start --> <DIV class=blk_18><A onmouseup=ISL_StopUp_1() class=LeftBotton onmousedown=ISL_GoUp_1() onmouseout=ISL_StopUp_1() href="javascript:void(0);" target=_self></A> <DIV class=pcont id=ISL_Cont_1> <DIV class=ScrCont> <DIV id=List1_1><!-- piclist begin --><A class=pl href=" http://info.china.alibaba.com/news/detail/v3000103-d1002527920.html" target=_blank><IMG height=72 alt=20-50元夏裝搶瘋 src=" http://www..com/img/_logo.gif" width=96>美女小凡最新夏裝</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v5000180-d1002468482.html" target=_blank><IMG height=72 alt=韓國人氣小耳環 src=" http://www..com/img/_logo.gif" width=96>韓國人氣小耳環</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v9-d1002531301.html" target=_blank><IMG height=72 alt=3萬6超值裝修88平 src=" http://www..com/img/_logo.gif" width=96>3萬6超值裝修88平</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v8-d1002530067.html" target=_blank><IMG height=72 alt=牛乾巴敲開財富門 src=" http://www..com/img/_logo.gif" width=96>牛乾巴敲開財富門</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v5003519-d1002285304.html" target=_blank><IMG height=72 alt=4K至7K高性能本本 src=" http://www..com/img/_logo.gif" width=96>4K至7K高性能本本</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v5001800-d1002511645.html" target=_blank><IMG height=72 alt=7萬裝修102平婚房 src=" http://www..com/img/_logo.gif" width=96>7萬裝修102平婚房</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v5003463-d1002527336.html" target=_blank><IMG height=72 alt=最新小車節油為主 src=" http://www..com/img/_logo.gif" width=96>最新小車節油為主</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v5003000-d1002505656.html" target=_blank><IMG height=72 alt="熱門項目 BT烤翅" src=" http://www..com/img/_logo.gif" width=96>熱門項目 BT烤翅</A><A class=pl href=" http://info.china.alibaba.com/news/detail/v3000103-d1002421590.html" target=_blank><IMG height=72 alt=流行T恤+褲裝 src=" http://www..com/img/_logo.gif" width=96>要顯瘦就這么穿!</A> <!-- piclist end --></DIV> <DIV id=List2_1></DIV></DIV></DIV><A onmouseup=ISL_StopDown_1() class=RightBotton onmousedown=ISL_GoDown_1() onmouseout=ISL_StopDown_1() href="javascript:void(0);" target=_self></A></DIV> <SCRIPT type=text/javascript> <!-- picrun_ini() //--> </SCRIPT> <!-- picrotate_left end --> </body> </html>
6. javascript讓圖片移動的函數
<img src="*.jpg" id="tImg" style="position:absolute"/><input type="button" value="開始滾動" onclick="move()" />
<script>
//使圖片運動的方法
function move(){
var imgLeft=img.offsetLeft;//取得圖像當前的left值
var speed=10;
if(imgLeft<1000){
img.style.left+=speed+"px"; //操作img的style屬性使之運動
setTimeout(move,50)//定時的調用當前這個方法
}
}
</script>