我想要实现的是有一个响应式水平和垂直滑块用于我的投资组合。问题出现在我有一个<div>
带有类 'project. 我似乎无法定位 each 的特定实例<div>
,即使带有 click 事件的元素位于要定位的元素内。
直观地解释:当用户单击“右”箭头时,JQuery 应计算<img>
单击箭头实例中 s 的数量并循环遍历它们。错误似乎出现在 JQuery 当前正在检查所有<div class=project">
s 中的总图像的地方。你可以在这里看到一个活生生的例子:
http://yaocho-digital.com/portfolio/
到目前为止,这是我的 JQuery:
$(document).ready(function() {
// Get amount of projects
var projectTotal = $('.project').length;
// Get window height
var windowHeight = $(window).height();
// Create 'clickCounter' variable, so as not to go over the project amount, set at 1 to reflect 'nth-child' value of projects created before
var clickCounter = 1;
// Animate each window height on 'down' click
$('.down').click(function() {
$('.container').animate({top: '-=' + windowHeight}, 'fast');
clickCounter = clickCounter + 1;
if (clickCounter > projectTotal) {
$('.container').animate({top: '0'}, 'fast');
clickCounter = 1;
}
});
// Hide all images
$('.project > img').hide();
// Get the amount of images in each project
var imageTotal = $('.project > img').length;
// Set the initial image at one, not zero, so that the variable can control 'nth-child'
var imageCounter = 1;
// Show first <img> of each project
$('.project > img:nth-child(' + imageCounter + ')').show();
// Set variable for retrieving parent of clicked 'next'
var parentElement = $(this).parent();
$('.next').click(function() {
$('' + parentElement + ' > img:nth-child(' + imageCounter + ')').hide();
$('' + parentElement + ' > img:nth-child(' + imageCounter + ')').next().show();
imageCounter = imageCounter + 1;
if (imageCounter > imageTotal) {
imageCounter = 1;
$('' + parentElement + ' > img').hide();
$('' + parentElement + ' > img:nth-child(' + imageCounter + ')').show();
}
console.log($(this).parent());
});
});