仅适用于 chrome,不适用于 Firefox 的 JavaScript 脚本。 (拖动div的脚本)

标签 javascript html css cross-browser

可能这是个小问题。但是我找不到原因。我制作了一个脚本来通过拖动来更改 div 的位置。该代码在 chrome 浏览器中运行良好。但是当我尝试在 Firefox 上测试它时,它不起作用。

var h = window.innerHeight|| document.documentElement.clientHeight || document.body.clientHeight;
        window.onload = function () {
// ------------------lock the div with mouse pointer--------------


// variable dragged is for identified that you are click on the button or not
var dragged = false,
y = 0,pointerDis = 0,
boxElement = document.getElementById('drag'),
drgElement = document.getElementById('titl');
            if (boxElement) {
// -----------------check whether the title div is holding by the mouse to lock it with mouse-------
	drgElement.addEventListener('mousedown', function() {
	    dragged = true;
        pointerDis = event.clientY -  parseInt(window.getComputedStyle(boxElement, null).getPropertyValue("top"));

});
//------------------check whether the title div is released to drop the div-------------------------
	document.addEventListener('mouseup', function() {
	dragged = false;
});
    document.addEventListener('mousemove', function () {
        y = event.clientY;
    	if(dragged == true)
    	{
            y = y -pointerDis;
            if(y<0)
            {
                y = 0;
            }
            else if(y > window.innerHeight - boxElement.offsetHeight)
            {
                y = window.innerHeight - boxElement.offsetHeight;
            }
    		boxElement.style.top = y + 'px';
    	}
    });
 }
};
.drg {
			position: absolute;
			top:0;
			right: 0;
			background: red;
			border-top-left-radius: 45px;
			border-bottom-left-radius: 45px;
		}
		#titl{
			background: blue;
			width: 50px;
			text-align: center;
			color: white;
			font-size: 30px;
			border-top-left-radius: 10px;
		}
		#det{
			background: #f9c500;
			width: 50px;
			border-bottom-left-radius: 10px;
			text-align: center;
		}
<!DOCTYPE html>
<html>
<head>
	<title>test 4</title>
</head>
<body>
<div class = "drg" id="drag">
	<div id="titl" unselectable="on" onselectstart="return false;">....</div>
	<div id="det">this is the details menu</div>
</div>
</body>
</html>

您可以通过单击并从蓝色 div 拖动它来沿 Y 轴拖动它。我不知道原因,或者我找不到在 Firefox 上修复这项工作的方法。请帮助我!

最佳答案

您必须捕获(mousemove 或 mousedown)事件作为包装函数的输入

drgElement.addEventListener('mousedown', function(event)...

var h = window.innerHeight|| document.documentElement.clientHeight || document.body.clientHeight;
        window.onload = function () {
// ------------------lock the div with mouse pointer--------------


// variable dragged is for identified that you are click on the button or not
var dragged = false,
y = 0,pointerDis = 0,
boxElement = document.getElementById('drag'),
drgElement = document.getElementById('titl');
            if (boxElement) {
// -----------------check whether the title div is holding by the mouse to lock it with mouse-------
	drgElement.addEventListener('mousedown', function(event) {
	    dragged = true;
        pointerDis = event.clientY -  parseInt(window.getComputedStyle(boxElement, null).getPropertyValue("top"));

});
//------------------check whether the title div is released to drop the div-------------------------
	document.addEventListener('mouseup', function() {
	dragged = false;
});
    document.addEventListener('mousemove', function (event) {
        y = event.clientY;
    	if(dragged == true)
    	{
            y = y -pointerDis;
            if(y<0)
            {
                y = 0;
            }
            else if(y > window.innerHeight - boxElement.offsetHeight)
            {
                y = window.innerHeight - boxElement.offsetHeight;
            }
    		boxElement.style.top = y + 'px';
    	}
    });
 }
};
.drg {
			position: absolute;
			top:0;
			right: 0;
			background: red;
			border-top-left-radius: 45px;
			border-bottom-left-radius: 45px;
		}
		#titl{
			background: blue;
			width: 50px;
			text-align: center;
			color: white;
			font-size: 30px;
			border-top-left-radius: 10px;
		}
		#det{
			background: #f9c500;
			width: 50px;
			border-bottom-left-radius: 10px;
			text-align: center;
		}
<!DOCTYPE html>
<html>
<head>
	<title>test 4</title>
</head>
<body>
<div class = "drg" id="drag">
	<div id="titl" unselectable="on" onselectstart="return false;">....</div>
	<div id="det">this is the details menu</div>
</div>
</body>
</html>

关于仅适用于 chrome,不适用于 Firefox 的 JavaScript 脚本。 (拖动div的脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47448630/

相关文章:

javascript - javascript中的原型(prototype)行为

javascript - "use strict"导致未定义的错误

javascript - 火狐插件 : How to insert an iFrame on page load based on current domain name

html - 如果放入一些长文本,DIV 不会扩展到内容

javascript - 如何将json从一种格式转换为另一种格式?

javascript - 附加脚本元素不请求 Google API

html - 定位旋转inline-block伪元素

html - 使用列表替换段落和标题标签

asp.net - 如何将作为 ASP.NET 图像控件提供的图像居中?

html - 图片在网站上不起作用?