圖片移動組件

來源:互聯網
上載者:User

標籤:style   blog   class   java   ext   color   

支援圖片移動,即左右上下移動,DEMO

 

 

 

<!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=utf-8" /><title>圖片移動</title><link rel="stylesheet" type="text/css" href="css/imgCss.css"></head><body onload="init();" style="background-color:#BFC9DA"><div style="margin:50px auto;width:500px;height:500px"><div style="font-size:24px;color:red;padding: 10px 150px;">圖片移動組件</div><div style="margin:10px"><input  type="text" id="picNumber" name="picNumber" value="1" style="width:40px"/><button type="button" onclick="addPic();">上傳圖片</button></div><div id="prodImg"></div></div><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script><script type="text/javascript">var imgArray = ["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg","img/5.jpg","img/6.jpg","img/7.jpg"];function init(){movePic.init();}function addPic(){var array = ["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg","img/5.jpg","img/6.jpg","img/7.jpg","img/8.jpg","img/9.jpg","img/10.jpg"];var number = parseInt($(‘#picNumber‘).val());var imgLength=imgArray.length;if((imgLength+number)>8){alert("最多隻能上傳八張圖片!");return ;}for(var i = 0;i<number;i++){imgArray.push(array[Math.floor(Math.random()*10)]);}imgLength = imgArray.length;movePic.init();}var movePic={init:function(){var imgHtml = [],imgLength = imgArray.length;for(var i=0;i<imgLength;i++){var operateHtml = [];operateHtml.push(‘<div class="prodImg" id="prodImg‘+(i+1)+‘"><img id="img‘+(i+1)+‘" class="imgcss"><div class="operate" id="operate‘+(i+1)+‘">‘);operateHtml.push(‘<span class="toleft" id="toleft‘+(i+1)+‘">左移</span>‘);if(i<4){operateHtml.push(‘<span class="todown" id="todown‘+(i+1)+‘">下移</span>‘);}else{operateHtml.push(‘<span class="toup" id="toup‘+(i+1)+‘">上移</span>‘);}operateHtml.push(‘<span class="toright"id="toright‘+(i+1)+‘">右移</span>‘);operateHtml.push(‘<span class="del" id="del‘+(i+1)+‘">刪除</span>‘);operateHtml.push(‘</div></div>‘);imgHtml.push(operateHtml.join(‘‘));}var prodImgHtml = [];prodImgHtml.push(‘<div style="width:480px;margin:5px;">‘);for(var i=0;i<imgLength;i++){prodImgHtml.push(imgHtml[i]);if(i==3){prodImgHtml.push(‘</div><div style="width:480px;margin:5px;">‘);}}$(‘#prodImg‘).html(prodImgHtml.join(‘‘));$(‘#toleft1‘).hide();$(‘#toleft5‘).hide();$(‘#toright4‘).hide();$(‘#toright8‘).hide();for(var i=0;i<imgLength;i++){$(‘#todown‘+(i+1)).hide();$(‘#toright‘+(i+1)).hide();$(‘#todown‘+(i-3)).show();if(i!=0&&i!=4){$(‘#toright‘+i).show();}}for(var i=0;i<imgLength;i++){$(‘#img‘+(i+1)).attr("src",imgArray[i]);this.bindImg(i+1);this.bindLeft(i);this.bindRight(i);this.bindDel(i);}for(var i=0;i<4;i++){this.bindDown(i);this.bindUp(i);}},bindImg:function(i){$(‘#prodImg‘+i).mouseenter(function(){$(‘#operate‘+i).show();});$(‘#prodImg‘+i).mouseleave(function(){$(‘#operate‘+i).hide()})},bindLeft:function(i){$(‘#toleft‘+(i+2)).click(function(){movePic.changeArray(i)})},bindDown:function(i){$(‘#todown‘+(i+1)).click(function(){movePic.downArray(i+1)})},bindRight:function(i){$(‘#toright‘+(i+1)).click(function(){movePic.changeArray(i)})},bindUp:function(i){$(‘#toup‘+(i+5)).click(function(){upArray(i+5)})},bindDel:function(i){$(‘#del‘+(i+1)).click(function(){delArray(i+1)})},changeArray:function(num){var temp = imgArray[num];imgArray[num] = imgArray[num+1];imgArray[num+1] = temp;this.init()},downArray:function(num){var newImgArray = []; var delImg = imgArray[num-1];imgArray.splice(num-1,1);for(var i = 0;i<imgArray.length;i++){newImgArray.push(imgArray[i]);if(i==num+2){newImgArray.push(delImg);}}imgArray = newImgArray;this.init();},upArray:function(num){var newImgArray = []; var delImg = imgArray[num-1];imgArray.splice(num-1,1);for(var i = 0;i<imgArray.length;i++){if(i==num-5){newImgArray.push(delImg);}newImgArray.push(imgArray[i]);}imgArray = newImgArray;imgLength = imgArray.length;this.init();},delArray:function(num){ imgArray.splice(num-1,1);imgLength = imgArray.length;this.init();}}</script></body></html>

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.