我的目的是在单击页面上的特定链接后显示一个弹出窗口。弹出窗口出现后,如果我单击其他地方,它会关闭(应该),但我被重定向到我的网站顶部。
为了避免重定向,是否可以稍微更改一下CSS代码?
还是我需要JS代码?
另一种选择是在链接中设置定位点。只有在没有其他方法可以通过 CSS 执行此操作时,我才会对其进行测试。
我的CSS代码在这里:
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.5);
transition: opacity 200ms;
visibility: hidden;
opacity: 0;
}
.overlay.light {
background: rgba(255, 255, 255, 0.5);
}
.overlay .cancel {
position: absolute;
width: 100%;
height: 100%;
cursor: default;
}
.overlay:target {
visibility: visible;
opacity: 1;
}
.popup {
margin: 75px auto;
padding: 20px;
background: rgb(37, 183, 211);
border: 1px solid #666;
width: 300px;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
position: relative;
}
.light .popup {
border-color: #aaa;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
}
.popup .close {
position: absolute;
width: 20px;
height: 20px;
top: 20px;
right: 20px;
opacity: 0.8;
transition: all 200ms;
font-size: 24px;
font-weight: bold;
text-decoration: none;
color: #666;
}
.popup .close:hover {
opacity: 1;
}
.popup .content {
max-height: 400px;
overflow: auto;
}
HTML链接在这里:
链接,更具体地说,请滚动到"收藏夹"选项卡,然后单击电影图标以查看弹出窗口。
我的建议是:不要使用伪元素:target
显示弹出窗口。
显示弹出窗口的最佳方式是使用 javascript 或更好的 JQuery,然后开发一个简单的脚本或使用插件。
这是一个您可以根据您的情况编辑和调整的示例: 小提琴
但是如果你想保留:target
你可以使用href="#fakelink"
而不是href="#"
.在此处查看演示:演示
在弹出窗口的纯 CSS 方法中,通过使用伪类:target
触发可见性。为了关闭弹出窗口,您导航到"#",这就是您被"重定向"到页面顶部的原因。
没有仅 CSS 的方法可以通过单击其他地方来隐藏弹出窗口。如果您必须使用与仅 CSS 弹出窗口相同的方法,您可以导航到页面的现有锚点或导航到页面<a class="cancel" href="#popup2closed"></a>
的不存在锚点并避免任何"重定向"。它只会使弹出窗口再次不可见。
在代码笔上搜索了许多选项后,我使其与JS一起使用:
.HTML:
<div id="ModalContentWrap" class="modal-wrap">
<div class="modal-content mix">
<section id="stigma-box">
<header>
<h1>21 Grams</h1>
<span class="avatar"><img src="image.jpg" alt="" /></span>
<h3><a href=" " target="blank"> ...</a></h3>
</header>
</section>
</div>
</div>
.CSS:
.modal-trigger {
cursor: pointer;
outline: 0 none;
}
.modal-wrap,
.modal-bg,
.modal-content {
display: none;
position: fixed;
}
.modal-wrap,
.modal-bg {
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.modal-wrap {
-webkit-backface-visibility: hidden;
-webkit-transform: translateZ(0);
}
.modal-bg {
background: rgba(0, 0, 0, .6);
}
.modal-content {
z-index: 1;
top: 50%;
left: 50%;
margin: -100px 0 0 -150px;
padding: 20px;
background: rgb(37, 183, 211);
border: 1px solid #666;
width: 300px;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
position: absolute;
}
.modal-close {
display: inline-block;
padding: 10px;
cursor: pointer;
}
.JS:
var mixModal = {
$bg: null,
$content: null,
init: function() {
// Instantiate MixItUp on background wrapper
this.$bg.mixItUp({
controls: {
enable: false
},
load: {
filter: 'none'
},
animation: {
effects: 'fade',
duration: 400,
}
});
// Instantiate MixItUp on content wrapper
this.$content.mixItUp({
controls: {
enable: false
},
animation: {
effects: 'fade translateZ(-300px) translateY(5%)',
duration: 300,
easing: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)'
},
load: {
filter: 'none'
}
});
},
// Create a "show" method
show: function() {
this.$bg.show().mixItUp('filter', 'all');
this.$content.show().mixItUp('filter', 'all');
},
// Create a "hide" method
hide: function() {
this.$bg.mixItUp('filter', 'none', function() {
$(this).hide();
});
this.$content.mixItUp('filter', 'none', function() {
$(this).hide();
});
}
};
// On document ready:
$(function() {
// Assign elements to modal properties
mixModal.$bg = $('#ModalBgWrap');
mixModal.$content = $('#ModalContentWrap');
// Initialize modal
mixModal.init();
// Bind click handlers
$('.modal-trigger').on('click', function() {
mixModal.show();
});
$('.modal-close, .modal-wrap').on('click', function(e) {
if (e.target === this) {
mixModal.hide();
}
});
});