作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要帮助来整理这段代码,目标是每个加载的页面显示数组图像 3 秒。
第一页正常。问题是在第一个页面之后加载的每个页面图像变化得更快。
<div data-role="page" id="page1" class="banners">
<div data-role="main" class="ui-content">
<div class="patrocinadores" align="center">
<img src="" width="100%" class="bannerPatrocinador">
</div>
</div>
</div>
<div data-role="page" id="page2" class="banners">
<div data-role="main" class="ui-content">
<div class="patrocinadores" align="center">
<img src="" width="100%" class="bannerPatrocinador">
</div>
</div>
</div>
<div data-role="page" id="page3" class="banners">
<div data-role="main" class="ui-content">
<div class="patrocinadores" align="center">
<img src="" width="100%" class="bannerPatrocinador">
</div>
</div>
</div>
<script>
$('.banners').on("pagecreate",function(event){
clearInterval(trocarImagem);
let cmpBanner1 = localStorage.getItem("cmpBanner1");
let cmpBanner2 = localStorage.getItem("cmpBanner2");
let cmpBanner3 = localStorage.getItem("cmpBanner3");
let cmpLink1 = localStorage.getItem("cmpLink1");
let cmpLink2 = localStorage.getItem("cmpLink2");
let cmpLink3 = localStorage.getItem("cmpLink3");
let imagens = ["adm/img/mkt/"+cmpBanner1, "adm/img/mkt/"+cmpBanner2+"", "adm/img/mkt/"+cmpBanner3+""];
let imagemAtual = 0;
function trocarImagem(){
imagemAtual = (imagemAtual + 1) % 3;
var teste = $('.bannerPatrocinador').attr('src',imagens[imagemAtual]);
}
setInterval(trocarImagem, 3000);
});
</script>
最佳答案
你的代码有几个问题:
工作示例:http://jsfiddle.net/ws5p6nbk/
var interval;
var imagemAtual = 0;
function trocarImagem() {
var imagens = ["https://s3.amazonaws.com/wmfeimages/wp-content/uploads/2018/09/27182802/4189366235_060e3e8e6f_z.jpg", "https://media.mnn.com/assets/images/2019/02/lexi.JPG.653x0_q80_crop-smart.jpg", "https://assets3.thrillist.com/v1/image/2709211/size/tmg-article_tall.jpg"];
console.log(imagemAtual);
imagemAtual = (imagemAtual + 1) % 3;
var activePage = $.mobile.activePage;
activePage.find('.bannerPatrocinador').attr('src', imagens[imagemAtual]);
}
$(document).on("pagecreate", '.banners', function(event) {
clearInterval(interval);
interval = setInterval(trocarImagem, 3000);
});
请测试这些更改,如果您满意,请接受答案。
关于javascript - 如何在加载了 jquery mobile 的每个页面上执行相同的 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54723004/
我是一名优秀的程序员,十分优秀!