简体   繁体   English

关闭模式窗口,页面跳到顶部(不需要)

[英]Close a modal window, and the page jumps to the top (not wanted)

I have a css modal window. 我有一个CSS模式窗口。 When I click the link, a modal window opens. 当我单击链接时,将打开一个模式窗口。 When I close the link, the window closes, and the page jumps all the way to the top of the page. 当我关闭链接时,窗口关闭,页面跳到页面顶部。 I need the page to stay where it is when the modal window is closed. 我需要页面保持在模式窗口关闭时的位置。

After doing some searching, I know this is because of the anchor tag being called in the close link: 经过一些搜索后,我知道这是因为在关闭链接中调用了定位标记:

<a class="modal-close" href="#" onclick="modal_close('ModalVid');">&times;</a>

(see some of the code below) (请参见下面的一些代码)

WHAT I'VE TRIED 我尝试过的
After some searching, I saw recommendations to add the preventDefault() function OR "return false". 经过一番搜索,我看到了建议添加preventDefault()函数或“ return false”。 So, I tried to add it to the onclick close function ... but neither worked. 因此,我试图将其添加到onclick关闭函数中……但都没有起作用。 I may not be using the two correctly (my knowledge of javascript is pretty limited) 我可能没有正确使用两者(我对javascript的了解非常有限)

PREVENTDEFAULT() PREVENTDEFAULT()

function modal_close(id) {
    var ModalVideo = document.getElementById(id);
    ModalVideo.pause();
        preventDefault();
} 

RETURN FALSE 返回假

function modal_close(id) {
    var ModalVideo = document.getElementById(id);
    ModalVideo.pause();
        return false
} 

Other recommendations said to try to add "overflow: hidden;" 据说其他建议尝试添加“溢出:隐藏”; to the body, but I think that recommendation only works for jquery bootstrap modals. 到身体,但我认为建议仅适用于jquery引导模态。

THE CODE 编码

I posted the modal code on codepen... https://codepen.io/jabbamonkey/pen/JjPMmKv 我在模笔上贴了模态代码... https://codepen.io/jabbamonkey/pen/JjPMmKv

HTML 的HTML

<a href="#modal1" id="modalclick1" onclick="modal_open('ModalVid');">
Open Video Window
</a>

<div id="modal1" class="modal-overlay">
  <div class="modal">
  <a class="modal-close" href="#" onclick="modal_close('ModalVid');">&times;</a>
    <div class="modal-content">
      <video controls id="ModalVid" >
        <source src="https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_1280_10MG.mp4" type="video/mp4">
      </video>
    </div>
  </div>
</div>

CSS 的CSS

.modal-overlay {
  position: fixed; top: 0; bottom: 0; left: 0; right: 0; 
  background: rgba(0, 0, 0, 0.7); transition: opacity 500ms; 
  visibility: hidden; opacity: 0;
}
.modal-overlay:target {
  visibility: visible; opacity: 1;
}

.modal {
  margin: 70px auto; padding: 20px; background: #fff; width: 40%;
  min-width:800px; position: relative; transition: all 5s ease-in-out;
}
.modal-close {
  position: absolute; top: 10px; right: 30px;
  transition: all 200ms; font-size: 30px;
  font-weight: bold; text-decoration: none;
  color: #fff; z-index:999999;
}
.modal-close:hover { color: #06D85F; }
.modal-content { max-height: 30%; overflow: auto; }
.modal-content video { width:100%; height:auto; }

JAVASCRIPT (doesn't affect the modal, but plays/stops the video onclick) JAVASCRIPT(不影响模式,但播放/停止点击视频)

// Stop Video on Close
function modal_open(id) {
    var ModalVideo = document.getElementById(id);
    ModalVideo.play();
}

function modal_close(id) {
    var ModalVideo = document.getElementById(id);
    ModalVideo.pause();
}

There are no error messages ... just the issue with the page jumping to the top. 没有错误消息……只是页面跳转到顶部的问题。

Well, the easiest fix is to get rid of the empty href (ie don't use href="#") and use an id tag (ie use href="#123"). 好吧,最简单的解决方法是摆脱空的href(即不要使用href =“#”)并使用id标记(即使用href =“#123”)。 This stopped the page from jumping (but be sure there is no anchor tag in the page with that same ID) 这样可以阻止页面跳转(但请确保页面中没有具有相同ID的锚标签)

Why don't you open and close the modal from javascript? 为什么不从javascript打开和关闭模式? In this case, remove href from the 'a' tag. 在这种情况下,请从“ a”标签中删除href。

const modal = document.getElementById("modal1");

When opening modal- 打开模态时

modal.style.visibility = "visible";
modal.style.opacity = 1;

When closing modal- 当关闭模态时

modal.style.visibility = "hidden";
modal.style.opacity = 0;

You can also modify the code to make modal reference dynamic. 您还可以修改代码以使模态参考动态化。 Updated pen- https://codepen.io/ashecret/pen/pozQWpp 更新了笔-https : //codepen.io/ashecret/pen/pozQWpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM