搜索
javascript - 为什么我弹出框每次要刷新后点击才能出来?
PHP中文网
PHP中文网 2017-04-10 16:31:23
[JavaScript讨论组]

<p style="width:200px; height:150px; border:1px solid red;bottom:-150px:position:fixed;right:10px;">

<button id="dd">按钮</button><button onclick="down()">关闭</button>

</p>
<script>
$('#dd').click(function(){

                                
             timer=setInterval(moveUp,100);
            
        });

    var p=document.querySelector("#dd");
    var timer=null;
    function moveUp(){                       
         var aa=
      document.defaultView.getComputedStyle(p,null);
      var bottom=parseInt(aa.bottom);
      if(bottom!=0){
         bottom=bottom+10;
         p.style.bottom=bottom+"px";}else{
           clearInterval(timer);
           timer=null;
         
         }
                      
 function moveDown(){               
        var aa=
      document.defaultView.getComputedStyle(p,null);
      var bottom=parseInt(aa.bottom);  
          if(bottom!=-($('#alertM').height())){
           bottom=bottom-10;
           p.style.bottom=bottom+"px";}else{

           clearInterval(timer);
           timer=null;
         
         } 
                                  
          }
      function down(){         //onclick 事件函数
          if(timer==null){
           timer=setInterval(moveDown,100);
            
          }                       
        }   
    }</script>

为什么我弹出框每次要刷新后点击才能出来,关闭后连续点击不会弹出来,需要刷新页面在点击

PHP中文网
PHP中文网

认证0级讲师

全部回复(3)
阿神

你点击 关闭 按钮 不是隐藏了你的p了吗?
p在页面上看不到了,你还要怎样~~~

if(bottom!=-($('#alertM').height())){
此处有误
获取的数据是带有px字符的,例如150px,所以要通过parseInt转成int

===下面是修改过后代样例代码===

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="../script/jquery-2.1.3.min.js"></script>
    <script>
        $(function(){
            $('#dd').click(function(){
                timer=setInterval(moveUp,100);
            });

            $('#closep').click(function(){
                down();
            });
            var p=document.querySelector("#popupp");
            var timer=null;
            function moveUp() {
                var aa = document.defaultView.getComputedStyle(p, null);
                var bottom = parseInt(aa.bottom);
                if (bottom != 0) {
                    bottom = bottom + 10;
                    p.style.bottom = bottom + "px";
                } else {
                    clearInterval(timer);
                    timer = null;
                }
            }
            function moveDown(){
                var aa=document.defaultView.getComputedStyle(p,null);
                var bottom=parseInt(aa.bottom);
                //aa.height 获取的数据是带有px字符的,例如150px,所以要通过parseInt转成int
                if(bottom!=-(parseInt(aa.height))){
                    bottom=bottom-10;
                    p.style.bottom=bottom+"px";
                }else{
                    clearInterval(timer);
                    timer=null;
                }
            }

            function down(){         //onclick 事件函数
                if(timer==null){
                    timer=setInterval(moveDown,100);
                }
            }

        });

    </script>
</head>
<body>
<button id="dd">show p</button>
<p id="popupp" style="width:200px; height:150px; border:1px solid red;bottom:-150px;position:fixed;right:10px;">
    <button>按钮</button><button id="closep" >关闭</button>
</p>

</body>
</html>
巴扎黑

建议上完整一点的代码,这样也能更快的解决问题

PHP中文网

我猜是这样的:

如果你弹出框的button id是dd,
但是你在代码里写的是

$('#dd').click(function(){})

这个是触发点击事件,所以每一次你刷新页面,都相当于定义了一下点击button后的操作。

所以你应该是监听这个click事件

$('#dd').on('click',function(){ 。。。。})
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号