js插件-图片椭圆轮播效果
2024-08-23 13:02:35
- 插件效果图:
- html 代码如下:
<div id="container">
<img src="data:images/cartoon/1.jpg" />
<img src="data:images/cartoon/2.jpg" />
<img src="data:images/cartoon/3.jpg" />
<img src="data:images/cartoon/4.jpg" />
<img src="data:images/cartoon/5.jpg" />
<img src="data:images/cartoon/6.jpg" />
<img src="data:images/cartoon/7.jpg" />
<img src="data:images/cartoon/8.jpg" />
<img src="data:images/cartoon/9.jpg" />
<img src="data:images/cartoon/10.jpg" />
<img src="data:images/cartoon/11.jpg" />
<img src="data:images/cartoon/12.jpg" />
<img src="data:images/cartoon/13.jpg" />
<img src="data:images/cartoon/14.jpg" />
<img src="data:images/cartoon/15.jpg" /> </div> - css代码如下:
* {
margin:;
padding:;
} body {
background-color: #efd;
}
#container {
width: 800px;
height: 400px;
position: relative;
margin: 50px auto;
} - js Carousel类代码:
var Carousel = function (options) { this.settings = {
imgs: [],
imgWidth: 150, //图片宽
imgHeight: 100, //图片高
time: 0,
rate: 0.5, //转动速度
containerId: "container", //包含图片容器id
containerWidth: 800, //包含图片容器宽
containerHeight: 300, //包含图片容器高
}; for (var item in options) { //extend
if (options.hasOwnProperty(item)) {
this.settings[item] = options[item];
}
} this.init.apply(this, arguments); //init
}; Carousel.prototype = { each: function (fn) {
for (var index = 0; index < this.settings.imgs.length; index++)
fn.call(this.settings.imgs[index], index);
},
init: function () {
var _this = this; this.settings.imgs = document.getElementById(this.settings.containerId).getElementsByTagName("img"); this.each(function (index) {
this.style.width = _this.settings.imgWidth + "px";
this.style.height = _this.settings.imgHeight + "px";
this.style.position = "absolute";
}); document.onmousemove = function (event) {
var event = event || window.event, positionX;
var positionX = _this.getPageX(event);
console.log(positionX);
_this.settings.rate = (positionX - document.body.clientWidth / 2) / (document.body.clientWidth / 2) * 0.25;
}
this.play();
},
getPageX: function (event) { if (event.pageX) {
return event.pageX;
} else {
return event.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft;
}
},
play: function () {
var _this = this;
setInterval(function () {
var that = _this.settings;
that.count = _this.settings.imgs.length;
that.time += that.rate * 40 / 1000;
_this.each(function (index) { //算法BaiDu所得
this.style.left = (Math.sin(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * (that.containerWidth - that.imgWidth) / 2 + "px";
this.style.top = (Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * (that.containerHeight - that.imgHeight) / 2 + "px";
this.style.width = (Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * that.imgWidth / 2 + that.imgWidth / 2 + "px";
this.style.height = (Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * that.imgHeight / 2 + that.imgHeight / 2 + "px";
this.style.zIndex = Math.floor((Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * 10);
})
}, 40);
}
}; - 最后 调用代码:
window.onload = function () {
new Carousel();
} - 页面最终代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
* {
margin: 0;
padding: 0;
} body {
background-color: #efd;
}
#container {
width: 800px;
height: 400px;
position: relative;
margin: 50px auto;
} </style>
<script>
var Carousel = function (options) { this.settings = {
imgs: [],
imgWidth: 150, //图片宽
imgHeight: 100, //图片高
time: 0,
rate: 0.5, //转动速度
containerId: "container", //包含图片容器id
containerWidth: 800, //包含图片容器宽
containerHeight: 300, //包含图片容器高
}; for (var item in options) { //extend
if (options.hasOwnProperty(item)) {
this.settings[item] = options[item];
}
} this.init.apply(this, arguments); //init
}; Carousel.prototype = { each: function (fn) {
for (var index = 0; index < this.settings.imgs.length; index++)
fn.call(this.settings.imgs[index], index);
},
init: function () {
var _this = this; this.settings.imgs = document.getElementById(this.settings.containerId).getElementsByTagName("img"); this.each(function (index) {
this.style.width = _this.settings.imgWidth + "px";
this.style.height = _this.settings.imgHeight + "px";
this.style.position = "absolute";
}); document.onmousemove = function (event) {
var event = event || window.event, positionX;
var positionX = _this.getPageX(event);
console.log(positionX);
_this.settings.rate = (positionX - document.body.clientWidth / 2) / (document.body.clientWidth / 2) * 0.25;
}
this.play();
},
getPageX: function (event) { if (event.pageX) {
return event.pageX;
} else {
return event.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft;
}
},
play: function () {
var _this = this;
setInterval(function () {
var that = _this.settings;
that.count = _this.settings.imgs.length;
that.time += that.rate * 40 / 1000;
_this.each(function (index) {
this.style.left = (Math.sin(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * (that.containerWidth - that.imgWidth) / 2 + "px";
this.style.top = (Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * (that.containerHeight - that.imgHeight) / 2 + "px";
this.style.width = (Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * that.imgWidth / 2 + that.imgWidth / 2 + "px";
this.style.height = (Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * that.imgHeight / 2 + that.imgHeight / 2 + "px";
this.style.zIndex = Math.floor((Math.cos(2 * Math.PI * that.time + 2 * Math.PI / that.count * index) + 1) * 10);
})
}, 40);
}
}; window.onload = function () {
new Carousel();
} </script> </head>
<body> <div id="container">
<img src="data:images/cartoon/1.jpg" />
<img src="data:images/cartoon/2.jpg" />
<img src="data:images/cartoon/3.jpg" />
<img src="data:images/cartoon/4.jpg" />
<img src="data:images/cartoon/5.jpg" />
<img src="data:images/cartoon/6.jpg" />
<img src="data:images/cartoon/7.jpg" />
<img src="data:images/cartoon/8.jpg" />
<img src="data:images/cartoon/9.jpg" />
<img src="data:images/cartoon/10.jpg" />
<img src="data:images/cartoon/11.jpg" />
<img src="data:images/cartoon/12.jpg" />
<img src="data:images/cartoon/13.jpg" />
<img src="data:images/cartoon/14.jpg" />
<img src="data:images/cartoon/15.jpg" /> </div> </body>
</html>
最新文章
- k近邻算法(knn)的c语言实现
- Web跨域问题总结
- SPOJ 3273
- Lua 之面向对象编程
- PAAS平台的web应用性能测试与分析
- javaScript基础练习题-下拉框制作(CSS)
- innodb_strict_mode
- 查找当前SQL Server下的Active Session正连接着哪个数据库
- SQL Server 中添加用户
- SQL Join(连接查询)
- Go语言语法汇总
- 初识动画animation
- swift3.0 从相册选取或者拍照上传图片至阿里云OSS
- destoon 开启邮箱
- “《编程珠玑》(第2版)第2章”:B题(向量旋转)
- 几大时尚前端UI框架的IE支持
- odoo10 addon开发流程
- 运维笔记--docker高效查看后台日志
- linux命令:查看系统版本
- Shell教程 之变量
热门文章
- WCF Error Handling
- Oracle命令行模式,批量执行SQL脚本
- php基础函数,数组
- Vagrant 手册之 Provisioning - file 配置程序
- Vagrant 手册之多个虚拟机 multi-machine
- C# DataTable删除行Delete与Remove的问题
- Vue Cli 3:vue.config.js配置文件
- poj1163The Triangle(动态规划,记忆化搜索)
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
- 读取交货单拣配数量PIKMG(转)