网站焦点图是一种网站内容的展现形式,可简单理解为一张图片或多张图片展现在网页上就是网站焦点图。在网站很明显的位置,用图片组合播放的形式,类似焦点新闻的意思只不过加上了图片。一般多网站首页版面或频道首页版面,因为是通过图片的形式,所以有一定的吸引性、视觉吸引性。容易引起访问者的点击,据国外的设计机构调查统计,网站焦点图的点击率明显高于纯文字,转化率高于使用在文字标题5倍。https://baike.baidu.com/item/%E7%BD%91%E7%AB%99%E7%84%A6%E7%82%B9%E5%9B%BE/?fr=aladdin
下面代码使用的是5张560*300的照片,如果你的照片大小不是这个尺寸的话建议修改里面的一些参数。为了更好的展示图片效果,建议使用相同尺寸的图片。
5张图片放载7次是为了按到最后一个跳到开始第一张的时候不那么明显的察觉到转换。
同时实现了无限循环播放,可以自定义播放间隔,当鼠标放在上面的时候停止自动播放,鼠标离开继续自动播放。
下面的小按钮能实现直接跳转图片。
<是左箭头,>是右箭头

细品代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>焦点图</title>
<style>
*{
margin: 0;
padding: 0;
text-decoration: none;
}
body{
padding: 20px;
}
#container{
width: 560px;
height: 300px;
border: 3px solid #333;
overflow: hidden;
position: relative;
}
#list{
width: 3920px;
height: 300px;
position: absolute;
z-index: 1;
}
#list img{
float: left;
}
#button{
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;
left: 200px;
}
#button span{
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
}
#button .on{
background: orangered;
}
.arrow{
cursor: pointer;
display: none;
line-height: 35px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 180px;
background-color: rgba(0,0,0,.3);
color: #fff;
}
.arrow:hover{
background: rgba(0,0,0,.7);
}
#container:hover .arrow{
display: block;
}
#prev{
left: 20px;
}
#next{
right: 20px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var container = document.getElementById("container");
var list = document.getElementById("list");
var buttons = document.getElementById("button").getElementsByTagName("span");
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var index = 1;
var animated = false;
var timer;
//显示小圆点
function showButton() {
for (var i = 0; i < buttons.length; i++){
if (buttons[i].className == "on"){
buttons[i].className = '';
break;
}
}
buttons[index-1].className = "on";
}
function animate(offset){
animated = true;
var newLeft = parseInt(list.style.left) + offset;
var time = 300; //位移总时间
var interval = 10; //位移间隔时间
var speed = offset / (time / interval);
function go(){
if ((speed < 0 && parseInt(list.style.left) > newLeft) || (speed > 0 && parseInt(list.style.left) < newLeft)){
list.style.left = parseInt(list.style.left) + speed + "px";
setTimeout(go, interval);
}
else{
animated = false;
list.style.left = newLeft + "px";
if (newLeft > -560){
list.style.left = -2800 + 'px';
}
if (newLeft < -2800){
list.style.left = -560 + "px";
}
}
}
go();
}
//自动播放
function play(){
timer = setInterval(function () {
next.onclick();
}, 3000);
}
//清除自动播放
function stop(){
clearTimeout(timer);
}
//点击下一个按钮
next.onclick = function () {
if (index == 5){
index = 1;
}
else{
index += 1;
}
showButton()
if (!animated){
animate(-560);
}
}
//点击上一个按钮
prev.onclick = function () {
if (index == 1){
index = 5;
}
else{
index -= 1;
}
showButton()
if (animated == false){
animate(560);
}
}
for (var i = 0; i < buttons.length; i++){
buttons[i].onclick = function () {
if (this.className == "on"){
return ;
}
var myIndex = parseInt(this.getAttribute('index'));
var offset = -560 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
}
}
container.onmouseover = stop;
container.onmouseout = play;
play();
}
</script>
</head>
<body>
<div id="container">
<div id="list" style="left: -560px">
<img src="images/5a.jpg" alt="">
<img src="images/1a.jpg" alt="">
<img src="images/2a.jpg" alt="">
<img src="images/3a.jpg" alt="">
<img src="images/4a.jpg" alt="">
<img src="images/5a.jpg" alt="">
<img src="images/1a.jpg" alt="">
</div>
<div id="button">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a class="arrow" id="prev"><</a>
<a class="arrow" id="next">></a>
</div>
</body>
</html>
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/96388.html