2016-08-29 86 views
1

我有一個JavaScript更改圖像scr,當你點擊它,它會循環。它也有導航鏈接和箭頭鍵盤導航。試圖將缺失的img屬性添加到自定義javascript

我很難添加圖像屬性的寬度,高度和alt到img scr。目前,當您查看最終結果時,它顯示:<img class="picture" src="http://i.imgur.com/tL6nW.gif" imageposition="1">

它目前缺少寬度,高度和替換值。在我的HTML內容中,我已經包含了寬度,高度和alt,但是javascript似乎沒有將這些屬性添加到最終結果中。任何人都可以幫我修復我的代碼?

/* 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'), function(idx, img) { 
 
    select.append('<option value="' + img.getAttribute('source') + '">Image ' + (idx + 1) + '</option>') 
 
    }); 
 

 
    select.on('change', function(e) { 
 
    $('.picture').attr({ 
 
     'src': e.target.options[e.target.selectedIndex].value, 
 
     '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, 0%, 20%, 1); 
 
} 
 
#pictureframe { 
 
    display: flex; 
 
    padding: 6px; 
 
    background-color: hsla(0, 0%, 40%, 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, 
 
#buttonright { 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
    flex-grow: 1; 
 
    padding: 6px; 
 
    color: hsla(40, 20%, 70%, 1); 
 
    font-variant: small-caps; 
 
    font-weight: bold; 
 
    font-family: Verdana, sans-serif; 
 
    background-color: hsla(0, 0%, 40%, 1); 
 
    cursor: pointer; 
 
} 
 
#buttonleft:hover, 
 
#buttonright:hover { 
 
    background-color: hsla(50, 50%, 40%, 1); 
 
} 
 
#shoebox { 
 
    display: none; 
 
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.js?bc893"></script> 
 
<div id="wall"> 
 
    <div id="pictureframe"> 
 
    <img class="picture" src=""> 
 
    </div> 
 
    <div id="buttonswrapper"> 
 
    <div id="buttonleft">prev</div> 
 
    <div id="buttonright">next</div> 
 
    </div> 
 
    <div id="jumpto"> 
 
    <select id="select-jump-to"></select> 
 
    </div> 
 
</div> 
 
<div id="shoebox"> 
 
    <!-- prevent loading all images by changing src to source --> 
 
    <img source="http://i.imgur.com/tL6nW.gif" width="100" height="100" alt="pic1"> 
 
    <img source="http://i.imgur.com/BfZ5f.gif" width="100" height="100" alt="pic2"> 
 
    <img source="http://i.imgur.com/mR7wo.gif" width="100" height="100" alt="pic3"> 
 
</div>

回答

2

對於這一點,你必須真正從你從你的<img source>標籤拉入你在調換<img>標籤<img>標籤插入屬性值。例如,在初始化你想改變你的代碼來做到這一點:你選擇圖像標籤本身,而不是源徹底

var leadOff = $('#shoebox img:first-child'); 

    $('.picture').attr({ 
    'src': leadOff.attr('source'), 
    'imageposition': '1', 
    'width': leadOff.attr('width'), 
    'height': leadOff.attr('height'), 
    'alt': leadOff.attr('alt') 
    }); 

注意。然後通過讀取標籤中的屬性,通過attr函數初始化屬性。您必須對上一個/下一個按鈕的點擊事件進行類似的調整,並更改選擇值分配以查看圖像對象的源屬性,例如, select.val(nextUp.attr('source'))

對於下拉列表中,您希望將這些屬性複製到自定義每個要(即數據寬度,數據高,數據ALT)拷貝過來屬性data-屬性,你可以稍後訪問:

$.each($('#shoebox img'), function(idx, img) { 
    select.append('<option value="' + 
    img.getAttribute('source') + 
     '" data-width="' + img.getAttribute('width') + 
     '" data-height="' + img.getAttribute('height') + 
     '" data-alt="' + img.getAttribute('alt') + 
     '">Image ' + (idx + 1) + '</option>' 
    ) 
    }); 

change情況下,剛剛從選項標籤的屬性讀取和設置它們的圖像:

select.on('change', function(e) { 
    var option = e.target.options[e.target.selectedIndex]; 
    $('.picture').attr({ 
     'src': option.value, 
     'imageposition': e.target.selectedIndex + 1, 
     'width': option.getAttribute('data-width'), 
     'height': option.getAttribute('data-height'), 
     'alt': option.getAttribute('data-alt') 
    }); 
    }); 
下面

更新代碼:

/* set first image in frame from #shoebox on document.ready */ 
 
$(function() { 
 
    var leadOff = $('#shoebox img:first-child'); 
 

 
    $('.picture').attr({ 
 
    'src': leadOff.attr('source'), 
 
    'imageposition': '1', 
 
    'width': leadOff.attr('width'), 
 
    'height': leadOff.attr('height'), 
 
    'alt': leadOff.attr('alt') 
 
    }); 
 

 
    var select = $('#select-jump-to'); 
 
    $.each($('#shoebox img'), function(idx, img) { 
 
    select.append('<option value="' + 
 
    img.getAttribute('source') + 
 
     '" data-width="' + img.getAttribute('width') + 
 
     '" data-height="' + img.getAttribute('height') + 
 
     '" data-alt="' + img.getAttribute('alt') + 
 
     '">Image ' + (idx + 1) + '</option>' 
 
    ) 
 
    }); 
 

 
    select.on('change', function(e) { 
 
    var option = e.target.options[e.target.selectedIndex]; 
 
    $('.picture').attr({ 
 
     'src': option.value, 
 
     'imageposition': e.target.selectedIndex + 1, 
 
     'width': option.getAttribute('data-width'), 
 
     'height': option.getAttribute('data-height'), 
 
     'alt': option.getAttribute('data-alt') 
 
    }); 
 
    }); 
 

 
}); 
 
/* 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 + ')'); 
 
    var select = $('#select-jump-to'); 
 
    if (imagePosition == imageTally) { 
 
    var leadOff = $('#shoebox img:first-child'); 
 
    $('.picture').attr({ 
 
     'src': leadOff.attr('source'), 
 
     'imageposition': '1', 
 
     'width': leadOff.attr('width'), 
 
     'height': leadOff.attr('height'), 
 
     'alt': leadOff.attr('alt') 
 
    }); 
 
    select.val(leadOff.attr('source')); //update the dropdown as well. 
 
    } else { 
 
    $('.picture').attr({ 
 
     'src': nextUp.attr('source'), 
 
     'imageposition': plusOne, 
 
     'width': nextUp.attr('width'), 
 
     'height': nextUp.attr('height'), 
 
     'alt': nextUp.attr('alt') 
 
    }); 
 
    select.val(nextUp.attr('source')); //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 + ')'); 
 
    var select = $('#select-jump-to'); 
 
    if (imagePosition == '1') { 
 
    var lastPic = $('#shoebox img:last-child'); 
 
    $('.picture').attr({ 
 
     'src': lastPic.attr('source'), 
 
     'imageposition': imageTally, 
 
     'width': lastPic.attr('width'), 
 
     'height': lastPic.attr('height'), 
 
     'alt': lastPic.attr('alt') 
 
    }); 
 
    select.val(lastPic.attr('source')); //update the dropdown as well. 
 
    } else { 
 
    $('.picture').attr({ 
 
     'src': nextUp.attr('source'), 
 
     'imageposition': minusOne, 
 
     'width': nextUp.attr('width'), 
 
     'height': nextUp.attr('height'), 
 
     'alt': nextUp.attr('alt') 
 
    }); 
 
    select.val(nextUp.attr('source')); //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, 0%, 20%, 1); 
 
} 
 
#pictureframe { 
 
    display: flex; 
 
    padding: 6px; 
 
    background-color: hsla(0, 0%, 40%, 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, 
 
#buttonright { 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
    flex-grow: 1; 
 
    padding: 6px; 
 
    color: hsla(40, 20%, 70%, 1); 
 
    font-variant: small-caps; 
 
    font-weight: bold; 
 
    font-family: Verdana, sans-serif; 
 
    background-color: hsla(0, 0%, 40%, 1); 
 
    cursor: pointer; 
 
} 
 
#buttonleft:hover, 
 
#buttonright:hover { 
 
    background-color: hsla(50, 50%, 40%, 1); 
 
} 
 
#shoebox { 
 
    display: none; 
 
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.js?bc893"></script> 
 
<div id="wall"> 
 
    <div id="pictureframe"> 
 
    <img class="picture" src=""> 
 
    </div> 
 
    <div id="buttonswrapper"> 
 
    <div id="buttonleft">prev</div> 
 
    <div id="buttonright">next</div> 
 
    </div> 
 
    <div id="jumpto"> 
 
    <select id="select-jump-to"></select> 
 
    </div> 
 
</div> 
 
<div id="shoebox"> 
 
    <!-- prevent loading all images by changing src to source --> 
 
    <img source="http://i.imgur.com/tL6nW.gif" width="100" height="100" alt="pic1"> 
 
    <img source="http://i.imgur.com/BfZ5f.gif" width="100" height="100" alt="pic2"> 
 
    <img source="http://i.imgur.com/mR7wo.gif" width="100" height="100" alt="pic3"> 
 
</div>

相關問題