简体   繁体   English

使用类型的第n个

[英]Using nth of type

I'm creating a website part of which consists of a long page containing full page slides. 我正在创建一个网站,其中包含一个包含整页幻灯片的长页面。 On each of these slides there will be 2 images which can be clicked to open a modal to view them. 在每个幻灯片上将有2个图像,可以单击这些图像打开模态以查看它们。

The problem I'm having with my modal is that I place it at a certain distance from the top and this applies to every instance of the modal- fine for the first slide but less so for each subsequent slide. 我对模态的问题是我将它放在距离顶部一定距离的位置,这适用于第一张幻灯片的模态的每个实例,但对于每个后续幻灯片都不太适用。

One way I have considered to try and tackle this is nth child. 我考虑过尝试解决这个问题的一种方法是第n个孩子。 I know that there will be 2 images on each slide so I can add 100% to 3 and 4, 200% to 5 and 6, etc.... 我知道每张幻灯片上会有2张图片,所以我可以100%添加到3和4,200%到5和6等等....

But....I don't seem to be able to get nth child working. 但是......我似乎无法让第n个孩子工作。 My css basically says: 我的css基本上说:

 .modalDialog { position: fixed; font-family: Arial, Helvetica, sans-serif; color:blue; left: 22%; width:80%; height:90%; top:103%; z-index: 9; opacity:0; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; pointer-events: none; } .modalDialog:nth-of-type(1){ color:yellow; top: 203%; } 
 <div id="slide1" class="slide"> <div class="title"> blah blah </div> <div class="tooltip"> <a href="#openModal"> <img src="ONE.png"> <p class="tooltiptext">yup</p> </a> </div> <div class="tooltip"> <a href="#openModal2"> <img src="two.png"> <p class="tooltiptext">aye..</p> </a> </div> <div id="openModal" class="modalDialog"> <div> <a href="#close" title="Close" class="close">X</a> <h2>descriptions</h2> <img src="ONE.png"> </div> </div> <div id="openModal2" class="modalDialog"> <div> <a href="#close" title="Close" class="close">X</a> <h2>desc</h2> <img src="two.png"> </div> </div> </div> 

With the slide number counting up and the openModal also continuously counting up (so slide3 has openModal5 and 6) 随着幻灯片数量的增加而openModal也在不断计数(所以slide3有openModal5和6)

Now the problem is. 现在的问题是。 I just can't seem to get nth of type or something I've tried, nth of child, to do anything. 我似乎无法得到任何类型的东西,或者我已经尝试过的东西,孩子的任何东西,做任何事情。 Never mind doing what I want and putting the modals for the subsequent pages in different places, I can't even get the second modal on the first page to change- as you see from the version above I'm trying to just get some response. 没关系做我想做的事情并将后续页面的模态放在不同的地方,我甚至无法在第一页上更改第二个模式 - 正如您从上面的版本中看到的那样我试图得到一些回复。

What am I doing wrong with this? 我做错了什么?

You can use attribute selector [id$="Modal1"] 你可以使用属性选择器 [id$="Modal1"]

[attr$=value] [ATTR $ =值]

Represents an element with an attribute name of attr and whose last value is suffixed by "value". 表示属性名称为attr且其最后一个值以“value”为后缀的元素。

 .modalDialog { position: fixed; font-family: Arial, Helvetica, sans-serif; color: blue; left: 22%; width: 80%; height: 90%; top: 103%; z-index: 9; opacity: 0; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; pointer-events: none; } /*Starts with string */ [id^="openModal"] { position: relative; top: 200%; z-index: 10; opacity:1 } /*Ends with string */ [id$="Modal1"] { background : red } [id$="Modal2"] { background : yellow } 
 <div id="slide1" class="slide"> <div class="title"> blah blah </div> <div class="tooltip"> <a href="#openModal"> <img src="ONE.png"> <p class="tooltiptext">yup</p> </a> </div> <div class="tooltip"> <a href="#openModal2"> <img src="two.png"> <p class="tooltiptext">aye..</p> </a> </div> <div id="openModal1" class="modalDialog"> <div> <a href="#close" title="Close" class="close">X</a> <h2>descriptions</h2> <img src="ONE.png"> </div> </div> <div id="openModal2" class="modalDialog"> <div> <a href="#close" title="Close" class="close">X</a> <h2>desc</h2> <img src="two.png"> </div> </div> </div> 

nth-of-type(n)计算标签类型(在同一级别上),而不是类:在您的情况下,它将选择第n个div标签,而不是使用类.modalDialog的第n个标签

I'd like to suggest doing this a different way, instead of creating a new style to select each nth-of-type like you do in the following code: 我想以不同的方式建议这样做,而不是创建一个新的样式来选择每个类型的n,就像你在下面的代码中所做的那样:

.modalDialog:nth-of-type(1){
    color:yellow;
    top: 203%;
}

The modal dialogue should set a margin for how far it should be from the top, like this: 模态对话应该设置一个距离顶部应该有多远的边距,如下所示:

.modalDialog{
    color:yellow;
    margin: 15% auto;
}

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

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