Отключить колесико от тела, но все же оно активно на модальном (без решения css)

71
6

Я использую скрипт для прокрутки прокрутки для автоматической прокрутки страницы, чтобы привязываться к событиям мыши. Когда модальная была открыта, мне нужно отключить события мыши на теле, но все же активно на модальном уровне. Очевидно, я не могу сделать это с overflow css. Я пробовал использовать:

$("body").bind("mousewheel", function() {
return false;
});

на событии click, но он также отключает событие mousewheel в модальном режиме, и я не знаю, как его решить

У вас есть какое-то решение? Заранее спасибо за помощь.

function autoscroll() {
var delay = false;
$(document).on('mousewheel DOMMouseScroll', function(event) {
if ( !event.target == $('.modal')){
event.preventDefault();
}
if (delay) return;
delay = true;
setTimeout(function() {
delay = false
}, 200)
var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail;
var a = document.getElementsByClassName("anchor");
if (wd < 0) {
for (var i = 0; i < a.length; i++) {
var t = a[i].getClientRects()[0].top;
if (t >= 20) break;
}
} else {
for (var i = a.length - 1; i >= 0; i--) {
var t = a[i].getClientRects()[0].top;
if (t < -20) break;
}
}
if (a[i] != undefined) {
$('html,body').stop(true, false).animate({
scrollTop: a[i].offsetTop
}, {
queue: false,
duration: 800
});
}
});
};
autoscroll();

$('.openmod').click(function(){
$('.modal').fadeIn(100);
});

body{
position: relative;
margin:0px
}
section{
height: 100vh;
}
#sec1{
background-color: black;
}
#sec1{
background-color: black;
}
#sec2{
background-color: red;
}
#sec3{
background-color: blue;
}

.modal{
position: fixed;
width:50%;
height: 80%;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: white;
overflow-y: scroll;
display: none;
}

.openmod{
position: fixed;
top:10px;
left:10px;
color: white;
cursor: pointer;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="anchor"></a>
<section id="sec1">

</section>
<a class="anchor"></a>
<section id="sec2">
</section>
<a class="anchor"></a>
<section id="sec3">
</section>
<div class="openmod">OPEN MODAL</div>
<div class="modal">On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.
</div>

спросил(а) 2017-06-05T18:50:00+03:00 3 года, 5 месяцев назад
0
Ваш ответ
Введите минимум 50 символов
Чтобы , пожалуйста,
Выберите тему жалобы:

Другая проблема