js css3圓圈內拖動氣泡圖標動畫效果



213 850 284



特效描述:js css3 拖動氣泡 圖標動畫效果,css3氣泡動畫,拖動氣泡、js固定在圓圈內的圖標拖動氣泡動畫效果

代碼結構

1. 引入CSS

<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Karla">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/icon?family=Material+Icons">

2. HTML代碼

      <div class="container">
         <div class="help">Drag me around!</div>
         <div class="plane"></div>
         <div class="app">
            <div class="view">
               <div class="title">TEST APP</div>
               <div class="item">Update</div><br>
               <div class="item">Send</div><br>
               <div class="item">Rename</div><br>
               <div class="item close">Exit</div>
            </div>
         </div>
      </div>
      <div class="info"></div>
<script>
//-------------------------------------------------------------------------------//
//--------------------------------| Settings |-----------------------------------//
//-------------------------------------------------------------------------------//
var gridSize = 10 //10x10
var iconList = [
   'bluetooth', 'brightness_high', 'directions_transit', 'settings',
   'event', 'headset', 'help', 'insert_chart', 'library_music'
]
//-------------------------------------------------------------------------------//
//-------------------------------| Initialize |----------------------------------//
//-------------------------------------------------------------------------------//
var html = setupHTML()
listenEvents()
//Send it
setInterval(() => {
   updatePosition()
   updateIconSizes()
}, 1000/60)
function setupHTML(){
   var html = {
      container: document.querySelector('.container'),
      plane: document.querySelector('.plane'),
      appClose: document.querySelector('.app .close')
   }
   for(var i = 0; i < gridSize; i++){
      var icons = ''
      for(var o = 0; o < gridSize; o++){
         icons += `
            <div class="icon">
               <div class="draw">
                  <i class="material-icons">${utilChoose(iconList)}</i>
               </div>
            </div>`
      }
      html.plane.innerHTML += `<div class="row">${icons}</div>`
   }
   html.icons = document.querySelectorAll('.icon')
   return html
}
function listenEvents(){
   var that = this
   for(var i = 0; i < html.icons.length; i++){
      html.icons[i].addEventListener('mouseup', function(e){
         that.eventIconClick(this, e)
      })
   }
   html.container.addEventListener('mousedown', function(e){ eventMouseDown(pos(e)) })
   html.container.addEventListener('mouseup', function(e){ eventMouseUp(pos(e)) })
   html.container.addEventListener('mouseleave', function(e){ eventMouseUp(pos(e)) })
   html.container.addEventListener('mousemove', function(e){ eventMouseMove(pos(e)) })
   html.appClose.addEventListener('click', function(e){ eventCloseApp() })
}
//-------------------------------------------------------------------------------//
//---------------------------------| Events |------------------------------------//
//-------------------------------------------------------------------------------//
function eventMouseUp(pos){
   if(mouse.state == 'up') return
   mouse.state = 'up'
}
function eventMouseDown(pos){
   mouse.state = 'down'
   mouse.pos.offset = utilTransfer(pos)
   mouse.pos.move = utilTransfer(pos)
}
function eventMouseMove(pos){
   if(mouse.state === 'up') return
   mouse.state = "move"
   mouse.pos.move = utilTransfer(pos)
}
function eventIconClick(ele, event){
   if(mouse.state === 'move') return
   ele.classList.add('open')
   html.container.classList.add('open')
   var box = ele.getBoundingClientRect()
   var contBox = html.container.getBoundingClientRect()
   mouse.pos.click = {
      x: mouse.pos.current.x - (box.left - contBox.left) + contBox.width/2 - box.width/2,
      y: mouse.pos.current.y - (box.top - contBox.top) + contBox.height/2 - box.height/2
   }
}
function eventCloseApp(pos){
   html.container.classList.remove('open')
   for(var i = 0; i < html.icons.length; i++){
      html.icons[i].classList.remove('open')
   }
}
//-------------------------------------------------------------------------------//
//-------------------------------| Positioning |---------------------------------//
//-------------------------------------------------------------------------------//
var mouse = {
   state: 'up',
   pos: {
      offset: {x: 0, y: 0},
      move: {x: 0, y: 0},
      current: {x: 0, y: 0},
      click: {x: 0, y: 0},
      old: {x: 0, y: 0}
   }
}
function updatePosition(){
   // This is going to get bumpy
   if(mouse.state == "move"){
      mouse.pos.current.x += mouse.pos.move.x - mouse.pos.offset.x
      mouse.pos.current.y += mouse.pos.move.y - mouse.pos.offset.y
      mouse.pos.offset = utilTransfer(mouse.pos.move)
      mouse.pos.click = utilTransfer(mouse.pos.current)
   }
   if(mouse.state == "up"){
      mouse.pos.current.x -= (mouse.pos.current.x - mouse.pos.click.x)/10
      mouse.pos.current.y -= (mouse.pos.current.y - mouse.pos.click.y)/10
   }
   var transform = `translateX(${mouse.pos.current.x}px) translateY(${mouse.pos.current.y}px)`
   html.plane.style.transform = transform
}
function updateIconSizes(){
   for(var i = 0; i < html.icons.length; i++){
      // position
      var contBox = html.container.getBoundingClientRect()
      var iconBox = html.icons[i].getBoundingClientRect()
      var iconCenter = {
         x: iconBox.left+iconBox.width/2,
         y: iconBox.top+iconBox.height/2
      }
      var contCenter = {
         x: contBox.left + contBox.width/2,
         y: contBox.top + contBox.height/2
      }
      var center = {
         x: (contCenter.x - iconCenter.x),
         y: (contCenter.y - iconCenter.y)
      }
      // Max distance is 150 or contBox.width
      var distance = Math.min(Math.floor(utilDistance({x:0, y:0}, center)), contBox.width/2)
      var percent = Math.min((1 - distance/(contBox.width/2))*1.5, 1)
      var iconDraw = html.icons[i].getElementsByClassName('draw')[0]
      iconDraw.style.transform = `translateX(-50%) translateY(-50%) scale(${percent}, ${percent})`
      iconDraw.style.opacity = percent
   }
}
function pos(e){
   var box = html.container.getBoundingClientRect()
   return {
      x: e.clientX - box.left,
      y: e.clientY - box.top
   }
}
//-------------------------------------------------------------------------------//
//---------------------------------| Utility |-----------------------------------//
//-------------------------------------------------------------------------------//
function utilChoose(arr){
   return arr[Math.floor(Math.random()*arr.length)]
}
function utilTransfer(obj){
   return JSON.parse(JSON.stringify(obj))
}
function utilDistance(pos1, pos2){
   return Math.sqrt(Math.pow((pos2.x - pos1.x),2) + Math.pow((pos2.y - pos1.y),2))
}</script>



用戶評論
大牛,別默默的看了,快登錄幫我點評一下吧!:)      登錄 | 注冊


熱門標簽: flash導航菜單 flash導航條 flash導航 flash菜單 flash下拉菜單 flash下拉導航菜單 h5動畫導航 h5動畫菜單 html5動畫導航 html5動畫菜單 收縮菜單 收縮導航 右鍵菜單 導航切換 菜單切換 浮動菜單 浮動導航 滑動導航菜單 滑動導航 滑動菜單 滾動導航菜單 滾動菜單 滾動導航 加載動畫 拖動 拖拽 拖動插件 拖拽插件 浮動 懸浮 懸浮層 懸浮框 浮動層 浮動框 浮動插件 懸浮插件 彈出層拖動 窗口 彈出 彈窗 彈出層 提示框 對話框 遮罩 遮罩層 彈窗插件 浮動提示框 h5彈窗動畫 html5彈窗動畫 切換按鈕 h5動畫 h5背景動畫 h5場景動畫 h53D動畫 h5界面動畫 html5動畫 表單 表單美化 表單插件 表單美化插件 導航菜單 導航條 菜單欄 導航欄 分類導航 響應式導航條 響應式導航 h5按鈕動畫 html5按鈕動畫 圖片拖動 圖片拖拽 浮動菜單 浮動導航 h5動畫導航 h5動畫菜單 html5動畫導航 html5動畫菜單 提示框/彈出層 圖標導航 圖標菜單 按鈕控制
?
×
×

注冊

官方QQ群

掃描上面二維碼加微信群

官方QQ群

jQuery/js討論群
群號:642649996
Css3+Html5討論群
群號:322131262

加群請備注:從官網了解到

老夫子电子