介绍常用js的字符串操作api ❤❤❤
js-api复习: 字符串操作
1.作用根据位置返回字符: var str=123456
var char=str.charAt(3); ->char=4
2.作用根据字符返回位置: var str=123456
var index=str.indexof('3') ->index=2
3.作用截取:slice函数(起始位置,[结束位置]) ; 结束位置可以不设置
var str ="woshishuaige";
console.log(str.slice(2,6)); ->"shis" (包头不包尾);
console.log(str.slice(2)); ->"shishuaige" (直接到最后);
4.作用字符串截取:substr(起始位置,[结束位置]) ;可以不设置
var str ="woshishuaige";
console.log(str.substr(2,6)); ->"shishu" (包头不包尾);
console.log(str.substr(2)); ->"shishuaige" (直接到最后);
介绍常用js的保留小数操作api ❤❤
1.通过 indexOf 返回小数点的位置 截取字符串
var str = "122340.12345";
alert(str.substr(0,str.indexOf('.')+3)); //122340.12
2.先乘以100 取整 然后 除以100
alert(parseInt(str*100) /100); //122340.12
3.保留 2位 小数
alert(str.toFixed(2) ); //122340.12
介绍常用js的大小写转换操作api ❤❤
1. toUpperCase:转大写
2.toLowerCase:转小写
实例:var str="woshishuaige";
str.toUpperCase() -> WOSHISHUAIGE;
缓动公式: leader = leader + (target - leader ) /10 ;
1 btn.onclick = function() {
2 setInterval(function(){
3 leader = leader + (target - leader )/10;
4 box.style.left = leader + "px";
5 },30)
6 }
offset家族 作用:可以获取元素的宽高,根据坐标设置效果 ;
属性 :
*1.offsetWidth:宽度 =真实宽度+边框+padding*
*2.offsetHeight:宽度 =真实高度+边框+padding*
使用:`document.getElementById(''xxx'').offsetWidth`
*3.offsetLeft:距离(上一个定位的父级)的左区间值,如果父级没有定位,以body为主, 距离=padding+宽度(边框不算)*
*3.offsetTop:道理同上*
使用:`document.getElementById(''xxx'').offsetLeft`
特效一:筋斗云跟随导航
代码:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{margin: 0; padding: 0;}
ul {list-style:none;}
body {
background-color: #000;
}
.nav {
width: 800px;
height: 42px;
background:url("images/rss.png") no-repeat right center #fff;
margin: 100px auto;
border-radius: 5px;
position: relative;
}
.cloud {
width: 83px;
height: 42px;
position: absolute;
top: 0;
left: 0;
background: url(images/cloud.gif) no-repeat;
}
.nav ul {
position: absolute;
top: 0;
left: 0;
}
.nav li {
float: left;
width: 83px;
height: 42px;
line-height: 42px;
text-align: center;
color: #000;
cursor: pointer;
}
</style>
</head>
<body>
<div class="nav" id="nav">
<span class="cloud" id="cloud"></span>
<ul>
<li>首页新闻</li>
<li>师资力量</li>
<li>活动策划</li>
<li>企业文化</li>
<li>招聘信息</li>
<li>公司简介</li>
<li>上海校区</li>
<li>广州校区</li>
</ul>
</div>
</body>
</html>
<script>
var cloud = document.getElementById("cloud"); // 云彩
var nav = document.getElementById("nav");
var lis = nav.children[1].children;
var current = 0; // 用于存放点击时候的 offsetLeft
//alert(lis.length);
for(var i=0; i<lis.length;i++) {
lis[i].onmouseover = function() {
// alert(this.offsetLeft);
target = this.offsetLeft; // 把左侧的位置 ,给 target
}
lis[i].onmouseout = function() {
target = current; // 鼠标离开 target 是 刚才我们点击的位置
}
lis[i].onclick = function() {
current = this.offsetLeft; // 点击的时候 吧当前位置 存贮一下
}
}
// 缓动公式
var leader = 0, target = 0;
setInterval(function(){
leader = leader + (target - leader ) / 10;
cloud.style.left = leader + "px";
},10);
</script>
特效二:鼠标点击跟随
效果:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#image {
width: 99px;
position: absolute;
top:0;
left: 0;
}
</style>
</head>
<body>
![](img.jpg)
</body>
</html>
<script>
var image = document.getElementById("image");
document.onclick = function(event) {
var event = event || window.event;
targetX = event.clientX - image.offsetWidth /2;
targetY = event.clientY - image.offsetHeight /2;
}
// 缓动
var leaderX = 0,
leaderY= 0,
targetX = 0,
targetY = 0;
setInterval(function() {
leaderX = leaderX + (targetX - leaderX) / 10;
leaderY = leaderY + (targetY - leaderY) / 10;
image.style.left = leaderX + "px";
image.style.top = leaderY + "px";
},10)
</script>
特效二:鼠标放大镜
效果:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {margin: 0;padding: 0;}
img {
vertical-align: top;
}
.box {
width: 350px;
height: 350px;
margin:100px;
border: 1px solid #ccc;
position: relative;
}
.big {
width: 450px;
height: 450px;
position: absolute;
top: 0;
left: 360px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
}
.mask {
width: 100px;
height: 100px;
background: rgba(255, 255, 0, 0.4);
position: absolute;
top: 0;
left: 0;
cursor: move;
display: none;
}
.small {
position: relative;
}
.big img {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div class="box" id="fdj">
<!--小盒子-->
<div class="small">
![](images/001.jpg)
<div class="mask"></div>
</div>
<div class="big">
![](images/0001.jpg)
</div>
</div>
</body>
</html>
<script>
var fdj = document.getElementById("fdj"); // 获得最大的盒子
var small = fdj.children[0]; // 获得small 小图片 350盒子
var big = fdj.children[1]; // 获得 大图片 800 盒子
var mask = small.children[1]; // 小的黄色盒子
var bigImage = big.children[0]; // 大盒子里面的图片
small.onmouseover = function() { // 鼠标经过显示出他们
mask.style.display = "block";
big.style.display = "block";
}
small.onmouseout = function() {
mask.style.display = "none";
big.style.display = "none";
}
// 鼠标在small 内移动
var x = 0;
var y = 0;
small.onmousemove = function(event) {
var event = event || window.event;
x = event.clientX - this.offsetParent.offsetLeft - mask.offsetWidth /2; // 再某个盒子内的坐标
//alert(this.offsetLeft);
y = event.clientY - this.offsetParent.offsetTop - mask.offsetHeight /2;
if(x < 0)
{
x = 0;
}
else if(x > small.offsetWidth - mask.offsetWidth)
{
x = small.offsetWidth - mask.offsetWidth;
}
if(y<0)
{
y = 0;
}
else if(y > small.offsetHeight - mask.offsetHeight)
{
y = small.offsetHeight - mask.offsetHeight;
}
mask.style.left = x + "px";
mask.style.top = y + "px";
/*计算 : 夫子 一顿吃 2个馒头 娇子 一顿 4个馒头
问 夫子今天吃了 3个馒头 娇子应该吃几个? */
/*计算出他们的倍数 4 / 2 2倍
3 * 2 == 6个 */
/* 大图盒子 / 小图盒子 倍数
我们 再小图移动的距离 * 倍数 == 大图的位置*/
bigImage.style.left = -x * big.offsetWidth /small.offsetWidth + "px";
bigImage.style.top = -y * big.offsetHeight /small.offsetHeight + "px";
}
</script>