如何在内联创建对象时引用父级?(How to reference parent in inline creation of objects?)
假设我有一个父/子关系ob对象并尝试内联创建一个父对象(我不确定这是正确的单词)。 是否可以在自己的创建代码中引用创建的父代?
Parent = new Parent( { Name = "Parent", Child= new Child(/*ReferenceToParent*/) });
Let's say I have a parent/child-relationship ob objects and try to create a parent object inline (I'm not really sure that's the right word). Is it possible to reference the created parent in its own creation code?
Parent = new Parent( { Name = "Parent", Child= new Child(/*ReferenceToParent*/) });
原文:https://stackoverflow.com/questions/6843923
最满意答案
这是你得到的问题。
CSS
风格@media only screen and (max-width:659px){}
将计算窗口屏幕的
max-width
,包括scrollbar
,但是当你打电话时$(window).width()
它将返回除
scrollbar
之外的窗口屏幕的宽度,当您的窗口屏幕(包括使用Chrome developer tools
检查它的scrollbar
)宽度介于659px
和676px
之间时,会导致您出现意外结果。您需要做的是使用
CSS
作为基础,通过在@media only screen and (max-width:659px){}
添加一些style
@media only screen and (max-width:659px){}
来定义宽度是否> 659px
,在您的情况下我正在改变z-index
从30
到35
来检测CSS
是否已定义width
已经< 660px
然后使用
jQuery
,你需要检查类.bar
的z-index
CSS值是30
(表示宽度> 659px)还是35
(表示另一个),然后使用它作为条件而不是使用$(window).width()
。以下是您的示例中的工作示例:
$(window).on('scroll', function () { var offset = $(window).scrollTop(); if ($('.bar').css('z-index') == '30' && offset > 10) { $('#nav-bar').addClass('slideDown'); } else { $('#nav-bar').removeClass('slideDown'); } });
ul li { display: inline-block; } .bar { top: -67px; width: 100%; height: 67px; opacity: 0.97; z-index: 30; position: fixed; border-bottom: solid 1px #2B292B; background-color: #FCFCFC; -webkit-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5); box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5); -webkit-transition: top 0.4s; -moz-transition: top 0.4s; -ms-transition: top 0.4s; -o-transition: top 0.4s; transition: top 0.4s; } .slideDown { top: 0; } body { height: 1500px; } @media only screen and (max-width: 659px) { .bar { top: 0; z-index: 35; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="nav-bar" class="bar"> <div id="nav-content" class="cf"> <div id="branding-wrap"> <a href="homeurl"><img src="/images/logo.png" id="logo"/></a> </div> <!-- End #branding-wrap --> <nav id="main-nav" class="right"> <ul> <li>menu-item</li> <li>menu-item</li> <li>menu-item</li> </ul> </nav> <a href="#mobile-nav"><img src="/images/menu.png" id="menu-icon" class="left"/></a> </div> <!-- End #nav-content --> </div> <!-- End #nav-bar -->
注意 :单击“整
Full Page
以便可以调整窗口宽度Here's the problem that you get. The
CSS
style@media only screen and (max-width:659px){}
will calculate the
max-width
of the window screen including thescrollbar
, but when you call$(window).width()
it will return the width of the window screen excluding the
scrollbar
, there what caused you to have unexpected results when your window screen (including thescrollbar
if you check it using theChrome developer tools
) width is between659px
&676px
.What you need to do is to use the
CSS
as your base to define if it width is> 659px
or not by adding somestyle
to the@media only screen and (max-width:659px){}
, in your case I was changing thez-index
from30
to35
to detect if theCSS
has defined that thewidth
is already< 660px
Then using the
jQuery
, you need to check whether thez-index
CSS value of class.bar
is30
(which means that the width is > 659px) or35
(which mean the other), then use it as the conditional if rather than using the$(window).width()
.Here's the working example from your example:
$(window).on('scroll', function () { var offset = $(window).scrollTop(); if ($('.bar').css('z-index') == '30' && offset > 10) { $('#nav-bar').addClass('slideDown'); } else { $('#nav-bar').removeClass('slideDown'); } });
ul li { display: inline-block; } .bar { top: -67px; width: 100%; height: 67px; opacity: 0.97; z-index: 30; position: fixed; border-bottom: solid 1px #2B292B; background-color: #FCFCFC; -webkit-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5); box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5); -webkit-transition: top 0.4s; -moz-transition: top 0.4s; -ms-transition: top 0.4s; -o-transition: top 0.4s; transition: top 0.4s; } .slideDown { top: 0; } body { height: 1500px; } @media only screen and (max-width: 659px) { .bar { top: 0; z-index: 35; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="nav-bar" class="bar"> <div id="nav-content" class="cf"> <div id="branding-wrap"> <a href="homeurl"><img src="/images/logo.png" id="logo"/></a> </div> <!-- End #branding-wrap --> <nav id="main-nav" class="right"> <ul> <li>menu-item</li> <li>menu-item</li> <li>menu-item</li> </ul> </nav> <a href="#mobile-nav"><img src="/images/menu.png" id="menu-icon" class="left"/></a> </div> <!-- End #nav-content --> </div> <!-- End #nav-bar -->
Note: Click the
Full Page
so you can resize the window width
相关问答
更多-
从您提供的示例开始,我进行了以下修改: 从HTML中删除了h1标记 将navigation的style属性值放入CSS 为div.menu ul元素添加了一些CSS 这是一个小提琴,包括所做的修改 笔记: JS代码没有被修改(它与你的例子的JS代码相同)。 使用JSFiddle时,不要忘记从“框架和扩展”列表中选择jQuery;) CSS: #navigation { background-color:#FFF; height:100%; width:250px; float ...
-
高度:0然后向下滑动(Height:0 and then slide down)[2024-03-28]
你可以设置style="display:none;" 最初的列表 这里是更新jsfiddle https://jsfiddle.net/2mxo7zdq/2/ you can set the style="display:none;" for the list initially here is the updates jsfiddle https://jsfiddle.net/2mxo7zdq/2/ -
滑出垂直导航(Slide out vertical navigation)[2022-04-09]
你可以在这里使用z-index和一些jQuery实现你想要做的事情。 当openall按钮被激活时,您可以将sidebar的z-index设置为较高的值(例如9999 ,并且可以在隐藏侧边栏时将z-index设置为0 。 这些是我在代码中修改过的两个jQuery函数: $("#hide-nav").click(function() { $("#newcontent").animate({'left':"30px"}, 250); $(".sublinks").hide(250); $ ... -
我相信你的问题在.rslides1_nav的这一行: background: transparent url("themes.gif") no-repeat left top; 尝试: background: transparent url("http://responsiveslides.com/with-captions/themes.gif") no-repeat left top; 或将该图像复制到您的本地,并使用适当的链接。 我一直在用小提琴搞乱,但它看起来并不正确。 希望这至少可以让你开始 ...
-
你可以使用css @media,这里是一个例子 @media (max-width:767px){ .class-name { width: 50% !important; } } max-width是此css将应用的最大屏幕宽度,您可以按最小宽度更改最大宽度并根据需要调整其值。 you can use css @media, here is an example @media (max-width:767px){ .class-name { width: 50% !imp ...
-
为什么我的滑动导航无法在特定窗口宽度下工作(Why is my slide down navigation not working in specific window width)[2024-01-30]
这是你得到的问题。 CSS风格 @media only screen and (max-width:659px){} 将计算窗口屏幕的max-width ,包括scrollbar ,但是当你打电话时 $(window).width() 它将返回除scrollbar之外的窗口屏幕的宽度,当您的窗口屏幕(包括使用Chrome developer tools检查它的scrollbar )宽度介于659px和676px之间时,会导致您出现意外结果。 您需要做的是使用CSS作为基础,通过在@media only ... -
你可以找到id为'sunset'的幻灯片索引。 如果你的flexslider HTML结构就像..........................
-
从窗口右侧滑动DIV(Slide DIV from right of window)[2023-06-09]
你可以尝试几种选择。 如果要使用.animate(),则需要更改其初始状态的位置。 因此,在你的CSS中,你需要将你的div的初始状态设置为不在页面之外的东西,比如 #slide { position: absolute; right: 999em; } 使用此设置,添加.animate()将为指定的值设置指定属性的动画,因此请在动画完成后指定div最终的位置。 $("#slide").delay(5000).animate({right: 0}, 500); 另一种方法是使用jQuery ad ... -
因为你已经拥有你的div的id,所以为什么不尝试通过它的id滚动到特定的div。 我在这里解释了这个: http : //usmansidea.blogspot.com/2013/07/jquery-make-html-pages-scroll-able.html 你需要做的是将以下函数放在$ .document.ready(function(){})中; function scrolView(i) { $('div').each(function () { ...
-
您正在调整窗口大小时添加单击事件。 这将多次将相同的函数绑定到click事件。 所以最好的选择是将事件绑定一次,这可以在就绪事件上完成。 在这个链接中,我通过向navi-btn添加一个类并检查它来限制resize事件本身的这个绑定。 $(".navi-btn").not(".binded").addClass("binded").click(function () { $(".navigation").slideToggle(); }); 希望这会给你一个想法。 谢谢, Jothi You are ...