【发布时间】:2021-09-09 15:58:20
【问题描述】:
当我打开模态页面时,我想创建一个指向数组中地址的链接。 但是,您可以转动循环以获取完整地址或仅获取最后一个地址。
请告诉我如何在运行 modal 1 时获取第一个地址,以及在按下 Next 按钮或第二个模式时如何获取第二个地址。
$(function(){
const TITLE_NUM = 1;
$(window).on('scroll.nav', function(){
var scrollTop = $(this).scrollTop();
if(scrollTop > $('.cp9th-gp-info').offset().top) {
$('.cp9th-nav').addClass('fixed');
}
else {
$('.cp9th-nav').removeClass('fixed');
}
}).trigger('scroll.nav');
var loadImg = function(node, src, callBack) {
if(src.length > 0) {
var image = new Image();
image.onload = function(){
node.attr('src', image.src);
callBack();
}
image.src = src;
}
else {
callBack();
}
};
var initModal = function() {
var modal = $('.cp9th-gp-modal');
var imgDir = '/cpn/9th/assets/img/season/modal/';
var charaIdx = {};
var nextMemberTable = {
'spica': [
'spica_01', 'spica_02'
],
}
var setCharaData = function(id) {
var team = id.split('_')[0];
var num = id.split('_')[1];
var idxList = charaIdx[team];
var imgList = [
[
'.cp9th-gp-modal-chara',
imgDir + 'chara/' + team + '_chara_' + num + '.png',
],
[
'.cp9th-gp-modal-text',
imgDir + 'text/' + team + '_txt_' + num + '.png',
],
];
//URL
let textData = [
'https://www.google.co.com/',
'https://stackoverflow.com/',
]
var imgNum = imgList.length;
for(var i = 0; i < imgList.length; i++) {
var node = imgList[i][0];
var src = imgList[i][1];
//The place to change.
$('.cp9th-gp-modal-play > a').attr('href', textData[i]);
loadImg($(node), src, function(){
imgNum--;
if(imgNum <= 0) {
if(modal.hasClass('on') == false) {
setTimeout(function(){
modal.addClass('on');
}, 100);
}
}
});
}
$('.cp9th-gp-modal-team .js-modal-btn > a').attr('href', "#detail"+ num);
$('.cp9th-gp-modal-play > a > img').attr('src', '/cpn/9th/assets/img/season/modal/btn_play_' + num +'.png');
var idx = idxList.indexOf(id);
var nextIdx = idx + 1;
if(nextIdx >= idxList.length) nextIdx = 0;
var prevIdx = idx - 1;
if(prevIdx < 0) prevIdx = idxList.length - 1;
$('.cp9th-gp-modal-next > a').attr('href', idxList[nextIdx]);
$('.cp9th-gp-modal-prev > a').attr('href', idxList[prevIdx]);
var nextMemberList = nextMemberTable[team];
idx = nextMemberList.indexOf(id);
if(idx < nextMemberList.length - 1) {
$('.cp9th-gp-modal-btn').attr('href', nextMemberList[(idx + 1)]);
$('.cp9th-gp-modal-btn').removeClass('off');
}
else {
$('.cp9th-gp-modal-btn').addClass('off');
}
};
$('.cp9th-gp-team-list ul').each(function(){
$('a', this).each(function() {
var id = $(this).attr('href');
var team = id.split('_')[0];
if(typeof charaIdx[team] == 'undefined') {
charaIdx[team] = [];
}
charaIdx[team].push(id);
});
});
$('.cp9th-gp-modal-close, .cp9th-gp-modal-bg').on('click', function(){
Utility.scrollLock(false);
modal.removeClass('on');
});
$('.cp9th-gp-modal-btn, .cp9th-gp-modal-next > a, .cp9th-gp-modal-prev > a').on('click', function(){
setCharaData($(this).attr('href'));
return false;
});
$('.cp9th-gp-team-list ul').each(function(){
$('a', this).each(function(i){
$(this).data('idx', i);
});
$('a', this).on('click', function(){
Utility.scrollLock(true);
$('.cp9th-gp-modal-body .simplebar-content-wrapper').scrollTop(0);
setCharaData($(this).attr('href'));
return false;
});
});
};
initModal();
});
<ul>
<li><p>sample1</p><a href="spica_01"><img src="sample.png" alt=""></a></li>
<li><p>sample2</p><a href="spica_02"><img src="sample.png" alt=""></a></li>
</ul>
<div class="cp9th-gp-modal">
<div class="cp9th-gp-modal-bg"></div>
<div class="cp9th-gp-modal-close">
<img src="/cpn/9th/assets/img/season/modal/btn_close.png" alt="">
</div>
<div class="cp9th-gp-modal-body" data-simplebar>
<dl>
<dt>
<img src="" alt="" class="cp9th-gp-modal-chara">
<div class="cp9th-gp-modal-team">
<div class="js-modal-btn">
<a href="">
<img src="/cpn/9th/assets/img/season/btn_line.png" alt="">
</a>
</div>
</div>
<div class="cp9th-gp-modal-prev">
<a href="">
<img src="/cpn/9th/assets/img/season/modal/btn_left.png" alt="">
</a>
</div>
<div class="cp9th-gp-modal-next">
<a href="">
<img src="/cpn/9th/assets/img/season/modal/btn_right.png" alt="">
</a>
</div>
<div class="cp9th-gp-modal-next">
<a href="">
<img src="/cpn/9th/assets/img/season/modal/btn_right.png" alt="">
</a>
</div>
<img class="twitter_txt" src="/cpn/9th/assets/img/season/modal/txt_tweet.png" alt="">
<div class="cp9th-gp-modal-twitter">
<a href="">
<img src="/cpn/9th/assets/img/season/modal/btn_tweet.png" alt="">
</a>
</div>
</dt>
<dd>
<img src="" alt="" class="cp9th-gp-modal-text">
<div class="cp9th-gp-modal-play">
<a href="">
<img src="/cpn/9th/assets/img/season/modal/btn_play_01.png" alt="">
</a>
</div>
</dd>
</dl>
</div>
</div>
$('.cp9th-gp-modal-play > a').attr('href', textData[i]); 我想添加一个链接地址(textData)。 我不应该使用 for 循环吗? html模态代码很长,所以我只标记了那部分。 谢谢你帮助我。
【问题讨论】:
-
“一个接一个”是什么意思——您希望它作为动画播放?
-
感谢您的评论!!我想添加一个链接网址。这不是动画。
-
如果你想根据参数添加url,那你为什么不删除外观,只使用这一行
$('.cp9th-gp-modal-play > a').attr('href', textData[i]);并将变量i替换为参数? -
对不起,我不明白...这不是我制作的代码。你能告诉我怎么做吗?
-
哦,你是对的@JDBstillremembersMonica
标签: javascript arrays for-loop