简体   繁体   English

angular2:CSS目标选择器不起作用

[英]angular2: css target selector not working

I'm trying to follow this example( http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/ ) to create my own modal in an Angular2 project. 我正在尝试按照此示例( http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/ )在Angular2项目中创建自己的模态。

In my html template, I have 在我的html模板中,我有

 <a href="#openModal" (click)="openDialog()" >Open Modal again</a>

<div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>
        <h2>Modal Box</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

The reason that I add (click)="openDialog()" to <a> is because href="#openModal" is not registered to the router. 我将(click)="openDialog()"<a>的原因是因为href="#openModal"未注册到路由器。 I have to prevent the default behavior in the method. 我必须防止该方法中的默认行为。

And, in the corresponding scss file, I have 而且,在相应的scss文件中,

.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}

.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}

.modalDialog > div {
    width: 400px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
}

In the component's ts file, I have the following method binding to the click event of my link. 在组件的ts文件中,我将以下方法绑定到链接的click事件。

openDialog(){
    event.preventDefault();
}

The above code snippet is to popup a modal after user clicks the link; 上面的代码段是在用户单击链接后弹出一个模式。 however, nothing happended after doing it. 但是,这样做之后什么也没发生。 I think the css .modalDialog:target selector is actually not working for some reasons so that the opacity has not been changed. 我认为css .modalDialog:target选择器实际上由于某些原因而无法工作,因此不透明度没有改变。

By calling preventDefault() you stop that element from becoming the target of the #hash . 通过调用preventDefault()您可以阻止该元素成为#hash的目标。

You'll have to remove the preventDefault() call in order to set #openModal as the target of the hash of the URL on click, and use the :target selector. 为了将#openModal设置为单击时URL哈希的目标,您必须删除preventDefault()调用,然后使用:target选择器。

The OP has stated that this works: OP表示这有效:

openDialog () {
    location.hash = "#openModal"
    event.preventDefault()
}

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

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