我有一个javascript,当你点击图像时它会改变图像scr,它将循环通过.它还有箭头键盘导航的分页链接.我想要做的是添加一个选择下拉列表框,允许我跳转到我想要的任何图像.我怎样才能做到这一点?我只想要一个没有提交按钮的选择下拉列表
/* set first image in frame from #shoebox on document.ready */
$(function() {
var leadOff = $('#shoebox img:first-child').attr('source');
$('.picture').attr({'src' : leadOff,'imageposition' : '1'});
});
/* load next image from #shoebox (click on image and/or next button) */
$('#pictureframe,#buttonright').click(function() {
var imageTally = $('#shoebox img').length;
var imagePosition = $('.picture').attr('imageposition');
var plusOne = parseInt(imagePosition) + 1;
var nextUp = $('#shoebox img:nth-child(' + plusOne + ')').attr('source');
if (imagePosition == imageTally) {
var leadOff = $('#shoebox img:first-child').attr('source');
$('.picture').attr({'src' : leadOff,'imageposition' : '1'});
} else {
$('.picture').attr({'src' : nextUp,'imageposition' : plusOne});
}
});
/* load previous image from #shoebox (click on prev button) */
$('#buttonleft').click(function() {
var imageTally = $('#shoebox img').length;
var imagePosition = $('.picture').attr('imageposition');
var minusOne = parseInt(imagePosition) - 1;
var nextUp = $('#shoebox img:nth-child(' + minusOne + ')').attr('source');
if (imagePosition == '1') {
var lastPic = $('#shoebox img:last-child').attr('source');
$('.picture').attr({'src' : lastPic,'imageposition' : imageTally});
} else {
$('.picture').attr({'src' : nextUp,'imageposition' : minusOne});
}
});
/* Add arrow keyboard navigation */
function GoToLocation(url)
{
window.location = url;
}
Mousetrap.bind("right",function() {
document.getElementById('buttonright').click();
});
function GoToLocation(url)
{
window.location = url;
}
Mousetrap.bind("left",function() {
document.getElementById('buttonleft').click();
});
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
}
#wall {
display: flex;
flex-direction: column;
padding: 6px;
background-color: hsla(0,0%,20%,1);
}
#pictureframe {
display: flex;
padding: 6px;
background-color: hsla(0,40%,1);
}
#pictureframe img {
display: flex;
width: 100px;
height: 100px;
}
#buttonswrapper {
display: flex;
flex-grow: 1;
}
#buttonleft,#buttonright {
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
padding: 6px;
color: hsla(40,70%,1);
font-variant: small-caps;
font-weight: bold;
font-family: Verdana,sans-serif;
background-color: hsla(0,1);
cursor: pointer;
}
#buttonleft:hover,#buttonright:hover {
background-color: hsla(50,50%,1);
}
#shoebox {
display: none;
}
最佳答案
我从一个空的下拉列表开始,使用列表中的图像源作为选项值自动填充自己,然后让下拉列表更改事件驱动器根据所选选项加载它所加载的图像.以下是我提出的设置:
var select = $('#select-jump-to');
$.each($('#shoebox img'),function(idx,img) {
select.append('
如果您希望Prex / Next按钮也更改下拉列表,请将下拉列表的值设置为等于您在事件中设置的imageURL,例如$( ‘#选择跳转到’).VAL(variableContainingnextImage)
这是您的代码同时包含下拉列表和按钮更改下拉列表.
/* set first image in frame from #shoebox on document.ready */
$(function() {
var leadOff = $('#shoebox img:first-child').attr('source');
$('.picture').attr({
'src': leadOff,'imageposition': '1'
});
var select = $('#select-jump-to');
$.each($('#shoebox img'),'imageposition': e.target.selectedIndex + 1
});
});
});
/* load next image from #shoebox (click on image and/or next button) */
$('#pictureframe,#buttonright').click(function() {
var imageTally = $('#shoebox img').length;
var imagePosition = $('.picture').attr('imageposition');
var plusOne = parseInt(imagePosition) + 1;
var nextUp = $('#shoebox img:nth-child(' + plusOne + ')').attr('source');
var select = $('#select-jump-to');
if (imagePosition == imageTally) {
var leadOff = $('#shoebox img:first-child').attr('source');
$('.picture').attr({
'src': leadOff,'imageposition': '1'
});
select.val(leadOff); //update the dropdown as well.
} else {
$('.picture').attr({
'src': nextUp,'imageposition': plusOne
});
select.val(nextUp);//update the dropdown as well.
}
});
/* load previous image from #shoebox (click on prev button) */
$('#buttonleft').click(function() {
var imageTally = $('#shoebox img').length;
var imagePosition = $('.picture').attr('imageposition');
var minusOne = parseInt(imagePosition) - 1;
var nextUp = $('#shoebox img:nth-child(' + minusOne + ')').attr('source');
var select = $('#select-jump-to');
if (imagePosition == '1') {
var lastPic = $('#shoebox img:last-child').attr('source');
$('.picture').attr({
'src': lastPic,'imageposition': imageTally
});
select.val(lastPic); //update the dropdown as well.
} else {
$('.picture').attr({
'src': nextUp,'imageposition': minusOne
});
select.val(nextUp); //update the dropdown as well.
}
});
/* Add arrow keyboard navigation */
function GoToLocation(url) {
window.location = url;
}
Mousetrap.bind("right",function() {
document.getElementById('buttonright').click();
});
function GoToLocation(url) {
window.location = url;
}
Mousetrap.bind("left",function() {
document.getElementById('buttonleft').click();
});
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
}
#wall {
display: flex;
flex-direction: column;
padding: 6px;
background-color: hsla(0,1);
}
#pictureframe img {
display: flex;
width: 100px;
height: 100px;
}
#buttonswrapper {
display: flex;
flex-grow: 1;
}
#jumpto {
display: flex;
justify-content: center;
align-items: center;
}
#buttonleft,1);
}
#shoebox {
display: none;
}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|