作者:黄欣豪972 | 来源:互联网 | 2023-09-05 16:17
I have a problem with a slider. When I load the first time my Web page, it shows a slider with some images. I have some links to change the images to show, so when I click, I want to display as a slider the other ones. The problem is that when I click on the new link, it doesn't use the jquery function to format the slider.
我有滑块问题。当我第一次加载我的网页时,它会显示带有一些图像的滑块。我有一些链接可以更改要显示的图像,所以当我点击时,我想显示其他的滑块。问题是当我点击新链接时,它不使用jquery函数来格式化滑块。
Here I paste some code so you can figure out:
在这里,我粘贴一些代码,以便您可以弄清楚:
And then the jquery:
然后是jquery:
$(document).ready(function()
{
var timeout, manualMode = false,
$slideshow = $('#slideshow'),
$items = $slideshow.find('li').hide(),
total = $items.length,
getItem = function($item, trav) {
var $returnItem = $item[trav]();
return $returnItem.length ?
$returnItem :
$items[(trav == 'next') ? 'first' : 'last']();
},
showItem = function($currentItem, $itemToShow) {
var $itemToShow =
$itemToShow || getItem($currentItem,'next');
$currentItem.fadeOut(300, function() {
$itemToShow.fadeIn(300, fadeCallback);
});
},
fadeCallback = function() {
if (manualMode) { return; }
var $this = $(this),
$next = getItem($this, 'next'),
num = $this.prevAll().length + 1;
// set the timeout for showing
// the next item in 5 seconds
timeout = setTimeout(function() {
showItem($this, $next);
}, 3000);
},
handleBtnClick = function(e) {
clearTimeout(timeout);
manualMode = true;
var $currentItem = $items.filter(':visible'),
$itemToShow = getItem($currentItem, e.data.direction);
showItem($currentItem, $itemToShow);
};
$items.eq(0).fadeIn(500, fadeCallback);
});
This works perfect for the first time I load the page, but when I change the content with this:
这在我第一次加载页面时非常有用,但是当我用这个更改内容时:
So basically, what I do is, after an onclick() event on some link, I change the html content of a div, but obviously, it doesn't use the jquery, as it wasn't loaded at first. Any idea on how to solve this?
所以基本上,我做的是,在某个链接上的onclick()事件之后,我更改了div的html内容,但显然,它不使用jquery,因为它最初没有加载。关于如何解决这个问题的任何想法?
If I use location.reload(); it shows the first items I had inside, so it doesn't work for me...
如果我使用location.reload();它显示了我里面的第一件物品,所以它对我不起作用......
EDIT:
Wait, I edit the jsFiddle...
编辑:等等,我编辑jsFiddle ...
1 个解决方案
1
You need to create an init method for your slideshow and recall that when you need to.
您需要为幻灯片创建一个init方法,并在需要时调用它。
I want to gag seeing how this js is set up so I will ignore it and post how I would have handled it:
我想看看如何设置这个js所以我会忽略它并发布我将如何处理它:
$(document).ready(function(){
slideshow.init();
$('#nuestra_tienda_show').on('click', function()
{
$('.containerGaleria').html('');
$(' ')
.prependTo('.containerGaleria');
$('#galeria_seleccionada').html('Nuestra tienda 2011');
slideshow.init();
});
});
var slideshow = {
timeout: null,
manualMode: false,
total: 0,
init: function(){
clearTimeout(this.timeout);
var items = $('#slideshow').find('li').hide();
this.total = items.length;
items.eq(0).fadeIn(500, this.fadeCallback);
},
getItem: function(item, trav) {
var returnItem = item[trav]();
return returnItem.length ?
returnItem :
items[(trav == 'next') ? 'first' : 'last']();
},
showItem: function(currentItem, itemToShow) {
var itemToShow =
itemToShow || this.getItem(currentItem,'next');
currentItem.fadeOut(300, function() {
itemToShow.fadeIn(300, this.fadeCallback);
});
},
fadeCallback: function() {
if (manualMode) { return; }
var $this = $(this),
next = getItem($this, 'next'),
num = $this.prevAll().length + 1;
// set the timeout for showing
// the next item in 5 seconds
timeout = setTimeout(function() {
this.showItem($this, next);
}, 3000);
},
handleBtnClick: function(e) {
clearTimeout(this.timeout);
this.manualMode = true;
var currentItem = items.filter(':visible'),
itemToShow = this.getItem(currentItem, e.data.direction);
this.showItem(currentItem, itemToShow);
},
handleBtnClick: function(e) {
clearTimeout(this.timeout);
this.manualMode = true;
var currentItem = items.filter(':visible'),
itemToShow = this.getItem(currentItem, e.data.direction);
this.showItem(currentItem, itemToShow);
};
};
Then you would just call slideshow.init()
when you want to re-evaluate the slideshow content.
然后,当您想要重新评估幻灯片显示内容时,您只需调用slideshow.init()。
I didn't thoroughly audit this but the general idea is to turn this massive chain of vars into a single js object and contain the whole thing.
我没有彻底审核这个,但一般的想法是将这个庞大的变量链变成一个单独的js对象并包含整个事物。
I cannot understand why you would write the contents of the slideshow like this in the click function. You're wiping the html of the container, creating the elements, then prepending them to the container and rewriting the title id with the same string over and over. Why not just set the html of the slideshow to the new images?
我不明白为什么你会在点击功能中写这样的幻灯片的内容。您正在擦除容器的html,创建元素,然后将它们预先添加到容器中,并反复使用相同的字符串重写标题ID。为什么不直接将幻灯片的html设置为新图像?
$('#slideshow').html('');