从头开始的基本 jQuery 轮播

发布于 2024-12-08 12:25:10 字数 1940 浏览 0 评论 0 原文

我会使用现成的 jQuery 插件之一,但它们都不符合我对这个特定站点的需求。

这是我到目前为止所拥有的代码:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript" src="js/pushState.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#nav a.slide").click(function() {
        var img = $(this).children('img').attr('src');
        $('#content').hide().css({ 'background':'url('+ img +') no-repeat 50% 50%' }).fadeIn('3000');
        var remApp = $(this).prev('a.slide');
        remApp.remove();
        $("#nav").append(remApp);
    });
});

</script>
<style type="text/css">
html, body {
padding: 0px;
margin: 0px;
}
#content {
position: absolute;
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
z-index: 1;
}
#nav {
position: absolute;
top: 70%;
z-index: 3;
}
#nav a.slide {
margin: 0 20px 0 0;
text-decoration: none;
}
#nav .slide img {
width: 100px;
height: 85px;
}
</style>

</head>

<body>
<div id="content">


</div>
<div id="social_links">

</div>
<div id="nav">

<a href="#" class="slide">
<img src="images/slide1.gif" />
</a>

<a href="#" class="slide">
<img src="images/slide2.jpg" />
</a>

<a href="#" class="slide">
<img src="images/slide3.jpg" />
</a>

<a href="#" class="slide">
<img src="images/slide4.jpg" />
</a>

<a href="#" class="slide">
<img src="images/slide5.png" />
</a>
</div>

</body>
</html>

这将获取您单击的图像并将其删除,然后将其附加到末尾。我需要它来拍摄之前的每张图像并附加它们。

而且,它似乎有一个一次性规则。我可以让每个图像都播放一次,但然后什么也没有发生。

您可以在此处的“action”中看到它

I would use one of the readily available jQuery plugins, but none of them fit my needs for this particular site.

This is the code that I have so far:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript" src="js/pushState.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#nav a.slide").click(function() {
        var img = $(this).children('img').attr('src');
        $('#content').hide().css({ 'background':'url('+ img +') no-repeat 50% 50%' }).fadeIn('3000');
        var remApp = $(this).prev('a.slide');
        remApp.remove();
        $("#nav").append(remApp);
    });
});

</script>
<style type="text/css">
html, body {
padding: 0px;
margin: 0px;
}
#content {
position: absolute;
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
z-index: 1;
}
#nav {
position: absolute;
top: 70%;
z-index: 3;
}
#nav a.slide {
margin: 0 20px 0 0;
text-decoration: none;
}
#nav .slide img {
width: 100px;
height: 85px;
}
</style>

</head>

<body>
<div id="content">


</div>
<div id="social_links">

</div>
<div id="nav">

<a href="#" class="slide">
<img src="images/slide1.gif" />
</a>

<a href="#" class="slide">
<img src="images/slide2.jpg" />
</a>

<a href="#" class="slide">
<img src="images/slide3.jpg" />
</a>

<a href="#" class="slide">
<img src="images/slide4.jpg" />
</a>

<a href="#" class="slide">
<img src="images/slide5.png" />
</a>
</div>

</body>
</html>

This takes the image that you click on and removes it, then appends it to the end. I need it to take each of the previous images and append them.

Also, it seems to have a one-time rule. I can make each of the images go once, but then nothing happens.

You can see it in "action" here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

メ斷腸人バ 2024-12-15 12:25:10

由于事件绑定到元素,并且当删除并再次添加元素时,事件不再绑定到元素,因此可能会出现两个问题(解决其中一个应该会有所帮助)。换句话说,您从 DOM 中删除该元素并删除附加的事件,但是当您再次将它们插入 DOM 中时,事件不会重新附加。

有两种方法可以修复它:

  • 不要将事件附加到元素,而是附加到容器,然后委托事件(使用 jQuery 的 .delegate() 函数)到您需要的元素,例如像这样:

    $("#nav").delegate('a.slide', 'click', function() {
        var img = $(this).children('img').attr('src');
        $('#content').hide().css({ 'background':'url('+ img +') 无重复 50% 50%' }).fadeIn('3000');
        var remApp = $(this).prev('a.slide');
        remApp.remove();
        $("#nav").append(remApp);
    });
    
  • 删除元素时,使用.detach()< /a>,而不是 .remove() jQuery 的函数 - 它将删除元素而不删除事件。正如 .remove() 的文档所述:

    <块引用>

    与 .empty() 类似,.remove() 方法从
    DOM。当您还想删除元素本身时,请使用 .remove()
    和里面的一切一样。除了元素本身之外,所有
    与元素关联的绑定事件和 jQuery 数据将被删除。
    要删除元素而不删除数据和事件,请使用 .detach()
    相反。

There are two possible issues (solving one of them should help) resulting from the fact, that the events are bound to the elements, and when the elements are removed and added again, the events are not bound to the elements any more. In other words you remove the element from the DOM along with deleting the events attached, but when you insert them in the DOM again, the events are not re-attached.

There are two ways you can fix it:

  • do not attach events to the elements, but to the container, and then delegate the events (using jQuery's .delegate() function) to the elements you need, eg. like that:

    $("#nav").delegate('a.slide', 'click', function() {
        var img = $(this).children('img').attr('src');
        $('#content').hide().css({ 'background':'url('+ img +') no-repeat 50% 50%' }).fadeIn('3000');
        var remApp = $(this).prev('a.slide');
        remApp.remove();
        $("#nav").append(remApp);
    });
    
  • when removing the elements, do it using .detach(), not .remove() jQuery's function - it will remove the elements without deleting the events. As .remove()'s documentation says:

    Similar to .empty(), the .remove() method takes elements out of the
    DOM. Use .remove() when you want to remove the element itself, as well
    as everything inside it. In addition to the elements themselves, all
    bound events and jQuery data associated with the elements are removed.
    To remove the elements without removing data and events, use .detach()
    instead.

笑红尘 2024-12-15 12:25:10

当您使用 .remove 时,您的事件处理程序将从幻灯片中删除。根据 jQuery 文档(重点是我的):

当您想要删除元素本身及其内部的所有内容时,请使用.remove()。除了元素本身之外,与元素关联的所有绑定事件和 jQuery 数据都将被删除。要删除元素而不删除数据和事件,请改用 .detach()

但我不会删除它或拆开它。只需追加它,jQuery 就会自动将它放在最后。由于您也想将所有以前的兄弟移动到末尾,因此您需要这样:

var remApp = $(this).prev('a.slide');
$("#nav").append(remApp.prevAll().andSelf());

请注意,我删除了 .remove 命令并添加了 .prevAll().andSelf()remApp.prevAll() 获取所有先前的同级(但不是 remApp),并且 .andSelf()remApp 重新加入到收藏。然后将整个集合附加到列表的末尾。

Your event handlers are being removed from the slides when you use .remove. Per the jQuery documentation (emphasis mine):

Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach() instead.

I wouldn't remove it or detatch it though. Just append it and jQuery will automatically put it at the end. Since you want to move all previous siblings to the end too, you want this:

var remApp = $(this).prev('a.slide');
$("#nav").append(remApp.prevAll().andSelf());

Note that I removed the .remove command and I added .prevAll().andSelf() to remApp. .prevAll() gets all previous siblings (but not remApp) and .andSelf() rejoins remApp to the collection. Then you append the whole collection to the end of the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文