jQuery初学者关于上下滑动效果的问题
所以我尝试创建上下滑动效果,如本例所示,
http://paulsturgess.co.uk/articles/show/83-jquery-text-slide-up-slide-down-effect
问题是,它在网络时显示文本页面打开。
我希望“段落”仅在鼠标悬停在其上方时显示,而不是在页面首次加载时显示。
我对 jQuery 完全陌生,但我想让它发挥作用。
帮助?
我的脚本
<script type="text/javascript">
$(function(){
$('.feature_box').showFeatureText();
})
$.fn.showFeatureText = function() {
return this.each(function(){
var box = $(this);
var text = $('p',this);
// text.css({ position: 'absolute', top: '57px' }).hide();
box.hover(function(){
text.slideDown("slow");
},function(){
text.slideUp("fast");
});
});
}
HTML 内容
<div>
<div class="feature_box" align="right">
<h2><a href="#">Cart Details</a></h2>
<p>
<a href="#">Excepteur sint occaecat cupidatat non proident. <BR />
Qui officia deserunt mollit anim id est laborum<br />
Excepteur sint occaecat cupidatat non proident. <BR />
Qui officia deserunt mollit anim id est laborum</a></p>
</div>
</div>
我需要进行哪些更改才能在页面首次加载时默认情况下不显示该段落?
另外,鼠标悬停效果是在 div
标签上还是在 p
标签上?我有点困惑。抱歉,我对这一切都很陌生。
[编辑]
我刚刚做了一些更改,当页面首次加载时它没有显示该段落。
以下行已被注释掉。
// text.css({ 位置: '绝对', 顶部: '57px' }).hide();
还是鼠标悬停在 DIV
标签或 P
标签上的效果?
[编辑2]
以下代码行是否包含 jQuery 的某些函数或什么?
text.css({ position: 'absolute', top: '57px' }).hide();
什么是“text.css”?
So I tried creating the slide up down effect like in this example,
http://paulsturgess.co.uk/articles/show/83-jquery-text-slide-up-slide-down-effect
The problem is, it's showing the text when the web page opens.
I want the "Paragraph" to be displayed ONLY when mouse is over it and NOT when the page first loads.
I am a completely new to jQuery, but I want to make it work.
Help?
My script
<script type="text/javascript">
$(function(){
$('.feature_box').showFeatureText();
})
$.fn.showFeatureText = function() {
return this.each(function(){
var box = $(this);
var text = $('p',this);
// text.css({ position: 'absolute', top: '57px' }).hide();
box.hover(function(){
text.slideDown("slow");
},function(){
text.slideUp("fast");
});
});
}
HTML content
<div>
<div class="feature_box" align="right">
<h2><a href="#">Cart Details</a></h2>
<p>
<a href="#">Excepteur sint occaecat cupidatat non proident. <BR />
Qui officia deserunt mollit anim id est laborum<br />
Excepteur sint occaecat cupidatat non proident. <BR />
Qui officia deserunt mollit anim id est laborum</a></p>
</div>
</div>
What changes do I make so that the paragraph doesn't appear by default when the page first loads?
Also, is the mouse-over effect on the div
tag or the p
tag? I am a bit confused. Sorry, I am really new to all this.
[EDIT]
I just made some changes, and it didn't display the paragraph when the page first loaded.
The following line was commented out.
// text.css({ position: 'absolute', top: '57px' }).hide();
Again is the mouse-over effect on DIV
tag or P
tag?
[EDIT 2]
Is the following line of code containing some function of jQuery or what?
text.css({ position: 'absolute', top: '57px' }).hide();
What is the "text.css"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是购物车的工作代码(请确保您按照您想要的方式编辑 CSS)。它与 < 上的代码相同strong>示例页面,其中粘贴了问题副本中的 HTML。
好的,现在让我们浏览一下代码。
以下是
中示例页面的原始代码
jsFiddle 示例
单步执行代码...
首先我们从内部调用该方法匿名函数使用:
由于
showFeatureText
是$('.feature_box')
的方法,我们知道每次在this
中使用< code>showFeatureText,this
引用具有feature_box
类的所有元素。在您的情况下,这是一个DIV
。文本框。现在让我们进入“showFeatureText”。它被用作一个简短的 jQuery 插件。它是 jQuery 的一种方法:
好的,必须包含
return this.each(function(){...})
,以便该函数能够与 jQuery 选择很好地配合。在本例中,我们的 jQuery 选择是一个带有feature_box
类的 DIV,但请注意,即使该函数作为一种方法添加到选择了许多元素的 jQuery 选择中,它也会起作用,因为它使用 <代码>返回this.each()。无论如何,知道必须包含此内容就足够了,并且它允许您将.showFeatureText
链接到$('.feature_box')
上。好的,继续。为了方便起见,这两行只是定义了两个局部变量。
box
是$(this)
,在本例中是。因此将其称为“盒子”是有意义的。
text 是 div 内的段落。这是因为
('p', this)
选择了this
内的所有段落,而this
是feature_box
分区这是 jQuery 的基本语法。要选择其中的所有内容,请使用:$(this, that)
。这有点令人困惑,因为要选择 ida
和 idb
你将使用$("#a, #b")
这完全是不同的。请注意引号。所以现在
box
与我们的大 div.feature_box
同义,而text
与内部文本同义。让我们继续:
我们只是将
feature_box
div 中的所有段落设为不可见。text
是段落。.css()
为它们分配 CSS 样式。它定位了他们。最后hide()
让它们不可见。 CSS 定位基本上在页面的整个生命周期中保持有效。如果没有它,段落将位于照片下方。好的,现在文本已全部隐藏,我们将向 div 添加一些事件处理程序。换句话说,我们希望将鼠标悬停在
this
(即$('.feature_box')
)上时,.feature_box
div ,然后就会出现文本。我们还想在不悬停时隐藏所有内容:请注意hover() 中有两个函数。第一个是当我们将鼠标悬停在
框
上时发生的情况。第二个是当我们鼠标移出时会发生什么。当我们将鼠标悬停在 div 上时,文本
会向下滑动。当我们停止悬停时,它会滑回来。就是这样。一个用于滑动内容的 jQuery 插件。
您可以在这个 jsFiddle 示例each的工作情况>。请注意所有三个图像都有自己的滑动文本。
参考资料:
jQuery 选择器
.hide()
.hover()
.slideDown()
.slideUp()
.each()
创作 jQuery 插件
Here is the working code for the shopping cart (make sure you edit the CSS to how you want it). It's the same code as on the example page, with the HTML from your question copy pasted in.
Ok, now let's go through the code.
Here is the original code from the example page in a
jsFiddle example
Stepping through the code...
First we call the method from inside an anonymous function using:
Since,
showFeatureText
is a method of$('.feature_box')
we know that every timethis
is used insdeshowFeatureText
,this
is refering to all the elements with thefeature_box
class. That is oneDIV
in your case. The box of text.Now let's step into `showFeatureText. It's being used as a short jQuery plugin. It is a method of jQuery:
Ok, the
return this.each(function(){...})
has to be included so that the function plays nicely with the jQuery selections. In this case our jQuery selection is one DIV with the classfeature_box
, but notice that this function would work even if it were added as a method to a jQuery selection that had many elements selected, since it usesreturn this.each()
. At any rate, it's enough to know that this has to be included, and it's what allows you to chain.showFeatureText
onto$('.feature_box')
. Ok, moving on.These two lines just define two local variables for our convenience.
box
is$(this)
which in this case is the<div class="feature_box">
. So it makes sense to call itbox
.text are the paragraphs inside the div. This is because
('p', this)
selects all the paragraphs withinthis
, andthis
is thefeature_box
div. It's basic syntax for jQuery. To select all of this within that use:$(this, that)
. It's a little confusing, since to select ida
and idb
you would use$("#a, #b")
which is completely different. Note the quotation marks.So now
box
is synonymous with our big div.feature_box
, andtext
is synonymous with the text inside.Let's move on:
We simply make all the paragraphs in the
feature_box
div invisible.text
are the paragaphs..css()
assigns CSS styles to them. It positions them. Finallyhide()
makes them invisible. The CSS positioning will remain in effect essentially throughout the life of the page. Without it, the paragraphs would be below the photo.Ok, so now that the text is all hidden, we're going to add some event handlers to our div. In other words we want to make it so that if we hover over
this
which is$('.feature_box')
, the.feature_box
div, then the text will appear. We also want to hide everything when we're not hovering:Notice how there are 2 functions in hover(). The first one is what happens when we mouse over the
box
. The second is what happens when we mouse out. When we hover over the div, thetext
slides down. When we stop hovering it slides back up.And that's it. A jQuery plugin for sliding content.
You can see
each
at work on this jsFiddle example. Note how all three images have their own sliding text.References:
jQuery selectors
.hide()
.hover()
.slideDown()
.slideUp()
.each()
authoring jQuery plugins
对于初学者来说,您应该首先隐藏
。
您的代码应如下所示:
Well for starters you
<p>
should be hidden first.<p style="display:none;">...</p>
Here is what your code should look like: