如何使图像滑块的项目符号在活动时看起来不同
How to make a bullet of an image slider look different while is active?
我已经有滑块工作了。如果你按下子弹,你会得到图像的位置,如果你不按任何子弹,一段时间后,图像无论如何都会改变。
所以现在我需要知道如何使当前处于活动状态的子弹看起来与其他子弹不同。如果您单击项目符号,我已经对其进行了编码。但我希望它在图像显示时有所不同。你用java这样做吗?
这是我正在使用的代码:
Javascript:
var imageCount = 1;
var total = 6;
function photo(x) {
var image = document.getElementById('img_slider');
imageCount = x;
image.src = "imagenes/img"+ imageCount +".png";
clearInterval(time);
time = window.setInterval(function photoA() {
var image = document.getElementById('img_slider');
imageCount = imageCount + 1;
if(imageCount > total){imageCount = 1;}
if(imageCount < 1){imageCount = total;}
image.src = "imagenes/img"+ imageCount +".png";
},18000);
}
var time = window.setInterval(function photoA() {
var image = document.getElementById('img_slider');
imageCount = imageCount + 1;
if(imageCount > total){imageCount = 1;}
if(imageCount < 1){imageCount = total;}
image.src = "imagenes/img"+ imageCount +".png";
},18000);
.CSS:
#slider {
position:relative;
overflow: hidden;
}
#img_slider {
position:relative;
height:100%;
width:100%;
}
#slider .bulletswrapper {
position: relative;
display: table;
margin: 0 auto;
text-align: center;
font-size: 0;
z-index: 1;
}
#slider div.bulletswrapper div {
border: none;
display: inline-block;
width: 11px;
height: 11px;
background-color: #CCCCCC;
font-size: 0;
margin: 2px 9px;
cursor: pointer;
border-radius: 11px;
box-shadow: inset 0 1px 3px #666666;
}
#slider div.bulletswrapper div:hover {
background-color: #e7e7e7;
box-shadow: inset 0 1px 3px -1px #666666;
}
#slider div.bulletswrapper div:active{
background-color: #1293dc;
box-shadow: inset 0 1px 3px -1px #28b4ea,0 1px 1px rgba(0,0,0,.5);
}
.HTML:
<div id="slider">
<img src="imagenes/img1.png" id="img_slider" alt="slider">
<div class="bulletswrapper">
<div id="1_b" onClick="photo(1)">1</div>
<div id="2_b" onClick="photo(2)">2</div>
<div id="3_b" onClick="photo(3)">3</div>
<div id="4_b" onClick="photo(4)">4</div>
<div id="5_b" onClick="photo(5)">5</div>
<div id="6_b" onClick="photo(6)">6</div>
</div>
</div>
我稍微清理了一下您的代码并添加了一个名为 ChangeActiveBullet
的新函数,该函数删除了以前的选择并更新了当前选择。我还引入了一个名为 selectedBullet
的新变量,它会根据需要进行更新(通过 setInterval
或 onclick
)。完整代码如下:
Javascript:
var imageCount = 1;
var total = 6;
var selectedBullet = 1;
function photo(x) {
var image = document.getElementById('img_slider');
ChangeActiveBullet(x);
imageCount = x;
image.src = "https://placehold.it/" + imageCount + "50x150/ff0000";
}
var time = window.setInterval(function photoA() {
var image = document.getElementById('img_slider');
imageCount = imageCount + 1;
if (imageCount > total) {
imageCount = 1;
document.getElementById((total - 1) + '_b').classList.remove('active');
}
if (imageCount < 1) {
imageCount = total;
}
image.src = image.src = "https://placehold.it/" + imageCount + "50x150/ff0000";
ChangeActiveBullet(imageCount);
}, 4000);
function ChangeActiveBullet(x) {
//Use the selectedBullet variable to find the bullet to remove the "active" class from.
var previousBullet = document.getElementById(selectedBullet + '_b');
previousBullet.classList.remove('active');
//Add the "active" class to the current selection.
var activeBullet = document.getElementById(x + '_b');
activeBullet.classList.add('active');
selectedBullet = x;
}
CSS:
#slider div.bulletswrapper div.active {
background-color: red;
}
下面的演示使用占位符图像,您可以在实现中明显更改这些图像。
小提琴演示
您可以通过设置或清除活动项目符号的样式来更改活动项目符号在photo()
和photoA()
函数中的显示。
像这样:
// clear and set bullet styles
for( i = 1; i <= total; i++ ) {
var bullet = document.getElementById( i+'_b');
bullet.style.backgroundColor = (i===x ? 'white' : '');
}
样本 JSFIDDLE
相关文章:
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 如何使删除线看起来像x
- Twitter Bootstrap 2.1.0子菜单在Responsive上看起来很混乱
- 用javascript创建一个看起来正常分布的模式
- PHP函数的返回看起来如何对AJAX和PHP有效
- 如何使Codeschools棱角分明的一面看起来相似
- HTML/JavaScript拖动&Drop-是否可以使重影图像*NOT*看起来'冲洗掉'
- 未捕获的语法错误:意外的令牌非法?看起来不错
- NodeJS readline有时会打印回第一个输入行(但并不总是,看起来是随机的)
- 如何检查html是否看起来'空'
- 看起来第一个console.log是'It’不太对
- 使用 CSS 或 javascript/jQuery,我会使用哪种方法来使我的网站的导航栏看起来更 3d-ish
- 如何使用CSS或JQuery使整个网页看起来更小(就像Chrome的缩小功能一样)
- 是否可以使用 DIV 而不是 iFrame,以便内容看起来像在同一页面中
- "消息:应为')'"但事实并非如此;看起来不对
- 如何使用python将大量unicode隐藏为char,使其看起来像字符串
- 相同的css和HTML代码,但在两页上看起来不同
- 如何使许多jQuery ajax调用看起来很漂亮
- 我让一个图像看起来像一个可点击的按钮,但现在当我点击它时,它不会起到超链接的作用
- 如何使图像滑块的项目符号在活动时看起来不同