首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stopPropagation有时不会

stopPropagation有时不会
EN

Stack Overflow用户
提问于 2022-07-31 15:27:39
回答 1查看 96关注 0票数 -1

我想做三件事:

  1. 防止输入在移动

上的自动聚焦

  1. 阻止网页在移动

上移动

  1. 允许触摸移动

为防止输入自动聚焦于移动: a)添加只读输入标签b)添加以防止网页在移动: a)函数preventBehavior(e) { e.preventDefault();};document.addEventListener(“触摸移动”,preventBehavior,{被动: false});在移动平台上允许触摸移动: a)函数allowBehavior(e){ e.stopPropagation() } let = allowBehavior); tp.addEventListener(“触摸移动”,allowBehavior);)

这一切似乎大部分时间都起作用。偶尔,当滚动这些元素时,整个屏幕会移动。我怎么才能阻止这种行为?

注意:带有class=' timepicker‘的输入由js时间选择器库使用,该库在li元素中放置下拉列表。请参阅链接并在移动设备上使用以复制行为。

下面是指向页面https://sailwbob.com/lagin/views/test_form.html的链接

代码语言:javascript
复制
<head>
    <title>Reservation</title>
    <link rel="stylesheet" href="../public/css/header.css">
    <link rel="icon" type="image/png" href="../favicon.png">
    <script src="../public/js/header.js" type="text/javascript"></script>
   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />

    
    <style>
    input[type="submit"] { -webkit-appearance:none; -webkit-border-radius:45%;-webkit-width:35vw; }
</style>
   
</head>


<body style="margin-top:0px;">
    <div class='container'>
        <img id='swb' src="../public/images/swb.png" alt="SwB">
        <div class='img-container'>
            <img class='lround small' src="../public/images/img-1.png" alt="img-1">
            <img class='small' src="../public/images/img-2.png" alt="img-2">
            <img class='small' src="../public/images/img-3.png" alt="img-3">
            <img class='small' src="../public/images/img-4.png" alt="img-4">
            <img class='small' src="../public/images/img-5.png" alt="img-5">
            <img class='rround small' src="../public/images/img-6.png" alt="img-6">
        </div>

        <link rel="stylesheet" href="../public/css/test.css">

        <div id='res'>
            <div id='left' class='cols'>
                <p class='narrow'>Date</p>
                <p class='bold'>May 11</p>
                <p class='narrow'>&nbsp;</p>
                <p class='narrow'>Reservation</p>
                <p class='narrow'>Sail Time</p>
                <p class='narrow'>Crew Time</p>
            </div>
            <div id='center' class='cols'>
                <p class='narrow'>Skipper</p>
                <p class='bold'>Bob Smith</p>
                <p class='narrow'>Start Time</p>
                <p class='narrow bold'>09:00 AM</p>
                <p class='narrow bold'>TBD</p>
                <div class='inputWrapper'>
                    <input id='timeStart' class='timepicker' type='text' name='startTime' readonly>
                    <p id='w0' class='warning'>enter availability</p>
                </div>
            </div>
            <div id='right' class='cols'>
                <p class='narrow'>Boat</p>
                <p class='bold'>Syrena 40</p>
                <p class='narrow'>End Time</p>
                <p class='narrow bold'>09:00 PM</p>
                <p class='narrow bold'>TBD</p>
                <div class='inputWrapper'>
                    <input id='timeEnd' class='timepicker' type='text' name='startTime' readonly>
                    <p id='w1' class='warning'>end time > start time</p>
                </div>
            </div>
        </div>
        <div id='bottom'>
            <p id='bottomLeft' class='narrow'>Crew Status</p>
            <p id="bottomRight" class='narrow bold'>OUT</p>
        </div>
        <div id='submit'>
            <div id='spacer'>&nbsp;</div>
            <div id='update'>
                <a style="text-decoration:none;" href="#" onclick="window.location='mailto:rslotpole@gmail.com ?subject=Note to Skipper'; return false;">
                    <img src="../public/images/email-48.png" style="width:48px;height:48px;border:0;">
                </a>
                <input type='submit' value='update'  />
                <a style="text-decoration:none" href="tel:(617) 943-5119">
                    <img src="../public/images/phone-48.png" style="width:48px;height:48px;border:0;">
                </a>
            </div>
        </div>
    </div>
        <link rel="stylesheet" href="../public/css/timepicker/jquery.timepicker133.min.css">
        <script src="../public/js/timepicker/jquery-3.6.0.js"></script>
        <script src="../public/js/timepicker/jquery.timepicker134.min.js"></script>
        <script src="../public/js/test.js"></script>

         <script>
    function preventBehavior(e) {
       e.preventDefault(); 
    };
    document.addEventListener("touchmove", preventBehavior, {passive: false});
    
    function allowBehavior(e){
        console.log(e.target)
       
       e.stopPropagation()
    }

    let tp = document.getElementsByClassName('ui-timepicker-viewport')

    
     tp[0].addEventListener("touchmove", allowBehavior);
    
    
    
    
    
    
    
    </script>


</body>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-08 12:27:05

尝试使用css:

代码语言:javascript
复制
.ui-timepicker-viewport {
  overscroll-behavior: none
}

body {
  overscroll-behavior: none
}

或者javascript:

代码语言:javascript
复制
[...document.getElementsByClassName('ui-timepicker-viewport')].forEach(element => element.style.overscrollBehavior = "none");
document.body.style.overscrollBehavior = "none";
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73184564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档