jQuery初学者关于上下滑动效果的问题

发布于 2024-09-15 08:30:45 字数 1788 浏览 3 评论 0原文

所以我尝试创建上下滑动效果,如本例所示,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-09-22 08:30:45

这是购物车的工作代码请确保您按照您想要的方式编辑 CSS)。它与 < 上的代码相同strong>示例页面,其中粘贴了问题副本中的 HTML。


好的,现在让我们浏览一下代码。

以下是
中示例页面的原始代码
jsFiddle 示例

单步执行代码...

首先我们从内部调用该方法匿名函数使用:

$('.feature_box').showFeatureText();

由于 showFeatureText$('.feature_box') 的方法,我们知道每次在 this 中使用< code>showFeatureText,this 引用具有 feature_box 类的所有元素。在您的情况下,这是一个 DIV 。文本框。

现在让我们进入“showFeatureText”。它被用作一个简短的 jQuery 插件。它是 jQuery 的一种方法:

$.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("fast");
        },function(){
            text.slideUp("fast");
        });
    });
}

好的,必须包含 return this.each(function(){...}) ,以便该函数能够与 jQuery 选择很好地配合。在本例中,我们的 jQuery 选择是一个带有 feature_box 类的 DIV,但请注意,即使该函数作为一种方法添加到选择了许多元素的 jQuery 选择中,它也会起作用,因为它使用 <代码>返回this.each()。无论如何,知道必须包含此内容就足够了,并且它允许您将 .showFeatureText 链接到 $('.feature_box') 上。好的,继续。

        var box = $(this);
        var text = $('p',this);

为了方便起见,这两行只是定义了两个局部变量。 box$(this),在本例中是

。因此将其称为“盒子”是有意义的。

text 是 div 内的段落。这是因为 ('p', this) 选择了 this 内的所有段落,而 thisfeature_box分区这是 jQuery 的基本语法。要选择其中的所有内容,请使用:$(this, that)。这有点令人困惑,因为要选择 id a 和 id b 你将使用 $("#a, #b") 这完全是不同的。请注意引号。

所以现在 box 与我们的大 div .feature_box 同义,而 text 与内部文本同义。

让我们继续:

text.css({ position: 'absolute', top: '57px' }).hide();

我们只是将 feature_box div 中的所有段落设为不可见。 text 是段落。 .css() 为它们分配 CSS 样式。它定位了他们。最后 hide() 让它们不可见。 CSS 定位基本上在页面的整个生命周期中保持有效。如果没有它,段落将位于照片下方。

好的,现在文本已全部隐藏,我们将向 div 添加一些事件处理程序。换句话说,我们希望将鼠标悬停在 this(即 $('.feature_box'))上时,.feature_box div ,然后就会出现文本。我们还想在不悬停时隐藏所有内容:

    box.hover(function(){
        text.slideDown("fast");
    },function(){
        text.slideUp("fast");
    });

请注意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:

$('.feature_box').showFeatureText();

Since, showFeatureText is a method of $('.feature_box') we know that every time this is used insde showFeatureText, this is refering to all the elements with the feature_box class. That is one DIV 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:

$.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("fast");
        },function(){
            text.slideUp("fast");
        });
    });
}

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 class feature_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 uses return 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.

        var box = $(this);
        var text = $('p',this);

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 it box.

text are the paragraphs inside the div. This is because ('p', this) selects all the paragraphs within this, and this is the feature_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 id a and id b you would use $("#a, #b") which is completely different. Note the quotation marks.

So now box is synonymous with our big div .feature_box, and text is synonymous with the text inside.

Let's move on:

text.css({ position: 'absolute', top: '57px' }).hide();

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. Finally hide() 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:

    box.hover(function(){
        text.slideDown("fast");
    },function(){
        text.slideUp("fast");
    });

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, the text 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

写下不归期 2024-09-22 08:30:45

对于初学者来说,您应该首先隐藏

...

您的代码应如下所示:

<script type="text/javascript">
    $.fn.showFeatureText = function() {
        return this.each(function(){    
            var box = $(this);
            var text = $('p',this);    
            box.hover(function(){
              text.slideDown("slow");
            },function(){
              text.slideUp("fast");
            });        
          });
    }

    $(document).ready(function() {
      $('.feature_box').showFeatureText();    
    });
</script>

<div>
    <div class="feature_box" align="right">
        <h2><a href="#">Cart Details</a></h2>
        <p style="display:none;">
        <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>

Well for starters you <p> should be hidden first. <p style="display:none;">...</p>

Here is what your code should look like:

<script type="text/javascript">
    $.fn.showFeatureText = function() {
        return this.each(function(){    
            var box = $(this);
            var text = $('p',this);    
            box.hover(function(){
              text.slideDown("slow");
            },function(){
              text.slideUp("fast");
            });        
          });
    }

    $(document).ready(function() {
      $('.feature_box').showFeatureText();    
    });
</script>

<div>
    <div class="feature_box" align="right">
        <h2><a href="#">Cart Details</a></h2>
        <p style="display:none;">
        <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文