Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions 1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
font-family: Arial, Helvetica, sans-serif;
}
.right-click-area {
width: 100%;
height: 100%;

color: white;
margin: auto;
display: flex;
position: relative;
}
.right-click-menu {
margin: 0;
padding: 0;
position: fixed;
list-style: none;
background: #000000;
border: 2px solid #ffffff;
border-radius: 2px;
display: none;
}
.right-click-menu.active {
display: block;
}
.right-click-menu li {
width: 100%;
padding: 10px;
box-sizing: border-box;
cursor: pointer;
font-size: 20px;
}
.right-click-menu li:hover {
background: #333333;
}
20 changes: 20 additions & 0 deletions 1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="1.css" rel="stylesheet">
</head>
<body>
<div class="right-click-area">
<ul class="right-click-menu">
<li id="l1">Выучить html</li>
<li id="l2">Выучить js</li>
<li id="l3">Выучить реакт</li>
<li id="l4">Выучить промисы</li>
</ul>
</div>
</body>
<script src="1.js"></script>
</html>
26 changes: 26 additions & 0 deletions 1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const menuArea = document.querySelector(".right-click-area");
const menu = document.querySelector(".right-click-menu");

menuArea.addEventListener( "contextmenu", event => {
event.preventDefault();
menu.style.top = `${event.clientY}px`;
menu.style.left = `${event.clientX}px`;
menu.classList.add("active");
if (`${event.clientX}` >= 1200) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У меня монитор 1920*1080. Поэтому меню исчезает уже при клике во второй половине экрана.
Необходимо было вычислять границу отрисовки в зависимости от размера области просмотра страницы и размеров самого меню.

menu.style.cssText = "pading: 0";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Неверная запись свойства padding
В случае, если меню вылезает за границы страницы, его нужно лишь немного подвинуть так, чтобы оно было видно полностью на странице, а не рисовать в верхнем левом углу страницы. Для этого необходимо вычислить, на сколько пикселей меню заходит за экран и затем - отодвинуть его вверх или влево.

}
if (`${event.clientY}` >= 700) {
menu.style.cssText = "pading: 0";
}
}, false);

document.addEventListener("click", event => {
if (event.button !== 2) {
menu.classList.remove("active");
menu.removeAttribute("pading")
}
}, false);

menu.addEventListener("click", event => {
event.stopPropagation();
}, false);
40 changes: 40 additions & 0 deletions 2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#main {
width: 1000px;
height: 500px;
background-color: gray;
overflow: hidden;
display: flex;
justify-content: space-around;
}

#obj1 {
width: 30px;
height: 50px;
background: green;
position : relative;

}

#obj2 {
width: 30px;
height: 50px;
background: red;
position : relative;

}

#obj3 {
width: 30px;
height: 50px;
background: blue;
position : relative;

}

#obj4 {
width: 30px;
height: 50px;
background: purple;
position : relative;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Позиционирование должно быть абсолютным


}
18 changes: 18 additions & 0 deletions 2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="2.css" rel="stylesheet">
</head>
<body>
<div id = "main">
<div id="obj1"></div>
<div id="obj2"></div>
<div id="obj3"></div>
<div id="obj4"></div>
</div>
</body>
<script src="2.js"></script>
</html>
127 changes: 127 additions & 0 deletions 2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const menuArea = document.querySelector("#main");
const menu1 = document.querySelector("#obj1");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У всех элементов одинаковая логика - можно было вешать обработчики делегированием


menuArea.addEventListener( "contextmenu", event => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Должен обрабатываться обычный клик

event.preventDefault();
menu1.style.top = `${event.clientY}px`;
menu1.style.left = `${event.clientX}px`;
menu1.classList.add("active");
}, false);

document.addEventListener("click", event => {
if (event.button !== 2) {
menu1.classList.remove("active");
menu1.removeAttribute("pading")
}
}, false);

menu1.addEventListener("click", event => {
event.stopPropagation();
}, false);


////////////////////////////////////////// 1


const menu2 = document.querySelector("#obj2");

menuArea.addEventListener( "contextmenu", event => {
event.preventDefault();
menu2.style.top = `${event.clientY}px`;
menu2.style.left = `${event.clientX}px`;
menu2.classList.add("active");
}, false);

document.addEventListener("click", event => {
if (event.button !== 2) {
menu2.classList.remove("active");
menu2.removeAttribute("pading")
}
}, false);

menu2.addEventListener("click", event => {
event.stopPropagation();
}, false);


////////////////////////////////////////// 2


const menu3 = document.querySelector("#obj3");

menuArea.addEventListener( "contextmenu", event => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем вешаете 4 обработчика на один и тот же контейнер ?

event.preventDefault();
menu3.style.top = `${event.clientY}px`;
menu3.style.left = `${event.clientX}px`;
menu3.classList.add("active");
}, false);

document.addEventListener("click", event => {
if (event.button !== 2) {
menu3.classList.remove("active");
menu3.removeAttribute("pading")
}
}, false);

menu3.addEventListener("click", event => {
event.stopPropagation();
}, false);


////////////////////////////////////////// 3



const menu4 = document.querySelector("#obj4");

menuArea.addEventListener( "contextmenu", event => {
event.preventDefault();
menu4.style.top = `${event.clientY}px`;
menu4.style.left = `${event.clientX}px`;
menu4.classList.add("active");
}, false);

document.addEventListener("click", event => {
if (event.button !== 2) {
menu4.classList.remove("active");
menu4.removeAttribute("pading")
}
}, false);

menu4.addEventListener("click", event => {
event.stopPropagation();
}, false);


////////////////////////////////////////// 3










window.addEventListener('load', function () { //все в один блок, т.к. вам же надо получить #obj ?
var O = document.getElementById('obj3'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Задавайте переменным осмысленные названия

X = menu1.style.left,
Y = menu1.style.top,mouseX=0,mouseY=0; //надо объявлять X/Y здесь, т.к они используются за пределами замыкания обработчика
window.addEventListener('mousemove', function (ev) {
ev = window.event || ev; //если window.event определен, то это IE<9, поддерживаем
X=ev.pageX;
Y=ev.pageY;
});

function move() { //зачем аргумент ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Движение должно начинаться только при клике на элемент

var p = 'px';
console.log(X,Y);
O.style.left = X + p;
O.style.top = Y + p;

setTimeout(move, 1);
}
move();

});