最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js 實現(xiàn)碰撞檢測的示例

 更新時間:2020年10月28日 12:03:44   作者:星輝  
這篇文章主要介紹了js 實現(xiàn)碰撞檢測的示例,幫助大家更好的制作js特效,美化自身網(wǎng)頁,感興趣的朋友可以了解下

碰撞檢測

目錄

  • 代碼實例
  • 與簡易拖拽的差異
  • 下載源碼鏈接 

代碼實例

<div id="box" style="background: #334;width: 100px;height: 100px;position: absolute;cursor: move;z-index: 999;"></div>
<div id="box2" style="background: green;width: 100px;height: 100px;position: absolute;top: 200px;left: 500px;"></div>

(function () {
	var dragging = false
	var boxX, boxY, mouseX, mouseY, offsetX, offsetY
	var box = document.getElementById('box')
	var box2 = document.getElementById('box2')
	var box2X, box2Y

	// 鼠標(biāo)按下的動作
	box.onmousedown = down
	// 鼠標(biāo)的移動動作
	document.onmousemove = move
	// 釋放鼠標(biāo)的動作
	document.onmouseup = up

	// 鼠標(biāo)按下后的函數(shù),e為事件對象
	function down(e) {
		dragging = true
		
		// 獲取元素所在的坐標(biāo)
		boxX = box.offsetLeft
		boxY = box.offsetTop

		// 獲取元素box2所在的坐標(biāo)
		box2X = box2.offsetLeft
		box2Y = box2.offsetTop

		// 獲取鼠標(biāo)所在的坐標(biāo)
		mouseX = parseInt(getMouseXY(e).x)
		mouseY = parseInt(getMouseXY(e).y)

		// 鼠標(biāo)相對元素左和上邊緣的坐標(biāo)
		offsetX = mouseX - boxX
		offsetY = mouseY - boxY
	}

	// 鼠標(biāo)移動調(diào)用的函數(shù)
	function move(e){
		if (dragging) {
			// 獲取移動后的元素的坐標(biāo)
			var x = getMouseXY(e).x - offsetX
			var y = getMouseXY(e).y - offsetY

			// 計算可移動位置的大小, 保證元素不會超過可移動范圍
			var width = document.documentElement.clientWidth - box.offsetWidth
			var height = document.documentElement.clientHeight - box.offsetHeight

			// min方法保證不會超過右邊界,max保證不會超過左邊界
			x = Math.min(Math.max(0, x), width)
			y = Math.min(Math.max(0, y), height)

			// 給元素及時定位
			box.style.left = x + 'px'
			box.style.top = y + 'px'

			// 碰撞檢測
			// x坐標(biāo)值的范圍判斷,y坐標(biāo)值的范圍判斷
			var judge_x = (x >= box2X - box2.offsetWidth) && (x <= box2X + box2.offsetWidth)
			var judge_y = (y >= box2Y - box2.offsetHeight) && (y <= box2Y + box2.offsetHeight)
			if (judge_x && judge_y) {
				console.log("碰撞到")
			}
		}
	}

	// 釋放鼠標(biāo)的函數(shù)
	function up(e){
		dragging = false
	}

	// 函數(shù)用于獲取鼠標(biāo)的位置
	function getMouseXY(e){
		var x = 0, y = 0
		e = e || window.event
		
		if (e.pageX) {
			x = e.pageX
			y = e.pageY
		} else {
			x = e.clientX + document.body.scrollLeft - document.body.clientLeft
			y = e.clientY + document.body.scrollTop - document.body.clientTop
		}
		return {
			x: x,
			y: y
		}
	}
})()

與簡易拖拽的差異

簡易拖拽的鏈接

碰撞檢測

// 碰撞檢測
// x坐標(biāo)值的范圍判斷,y坐標(biāo)值的范圍判斷
var judge_x = (x >= box2X - box2.offsetWidth) && (x <= box2X + box2.offsetWidth)
var judge_y = (y >= box2Y - box2.offsetHeight) && (y <= box2Y + box2.offsetHeight)
if (judge_x && judge_y) {
	console.log("碰撞到")
}

下載源碼鏈接

星輝的Github

以上就是js 實現(xiàn)碰撞檢測的示例的詳細內(nèi)容,更多關(guān)于js 碰撞檢測的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

华宁县| 珠海市| 宁德市| 霍城县| 白城市| 临海市| 柳州市| 石门县| 田阳县| 亚东县| 邢台县| 巴楚县| 惠东县| 闽侯县| 乌拉特中旗| 鄂尔多斯市| 保康县| 松阳县| 盐池县| 无棣县| 南投市| 惠安县| 三穗县| 泰来县| 岑溪市| 台中县| 朝阳县| 金堂县| 泊头市| 景德镇市| 内丘县| 盐城市| 施秉县| 涞水县| 胶州市| 宾阳县| 璧山县| 昭通市| 阿鲁科尔沁旗| 星子县| 怀化市|