介紹常用js的字符串操作api ???
js-api復(fù)習(xí): 字符串操作
1.作用根據(jù)位置返回字符: var str=123456
var char=str.charAt(3); ->char=4
2.作用根據(jù)字符返回位置: var str=123456
var index=str.indexof('3') ->index=2
3.作用截取:slice函數(shù)(起始位置,[結(jié)束位置]) ; 結(jié)束位置可以不設(shè)置
var str ="woshishuaige";
console.log(str.slice(2,6)); ->"shis" (包頭不包尾);
console.log(str.slice(2)); ->"shishuaige" (直接到最后);
4.作用字符串截取:substr(起始位置,[結(jié)束位置]) ;可以不設(shè)置
var str ="woshishuaige";
console.log(str.substr(2,6)); ->"shishu" (包頭不包尾);
console.log(str.substr(2)); ->"shishuaige" (直接到最后);
介紹常用js的保留小數(shù)操作api ??
1.通過 indexOf 返回小數(shù)點(diǎn)的位置 截取字符串
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位 小數(shù)
alert(str.toFixed(2) ); //122340.12
介紹常用js的大小寫轉(zhuǎn)換操作api ??
1. toUpperCase:轉(zhuǎn)大寫
2.toLowerCase:轉(zhuǎn)小寫
實(shí)例:var str="woshishuaige";
str.toUpperCase() -> WOSHISHUAIGE;
緩動(dòng)公式: 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家族 作用:可以獲取元素的寬高,根據(jù)坐標(biāo)設(shè)置效果 ;
屬性 :
*1.offsetWidth:寬度 =真實(shí)寬度+邊框+padding*
*2.offsetHeight:寬度 =真實(shí)高度+邊框+padding*
使用:`document.getElementById(''xxx'').offsetWidth`
*3.offsetLeft:距離(上一個(gè)定位的父級(jí))的左區(qū)間值,如果父級(jí)沒有定位,以body為主, 距離=padding+寬度(邊框不算)*
*3.offsetTop:道理同上*
使用:`document.getElementById(''xxx'').offsetLeft`
特效一:筋斗云跟隨導(dǎo)航
代碼:
<!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>活動(dòng)策劃</li>
<li>企業(yè)文化</li>
<li>招聘信息</li>
<li>公司簡(jiǎn)介</li>
<li>上海校區(qū)</li>
<li>廣州校區(qū)</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; // 用于存放點(diǎn)擊時(shí)候的 offsetLeft
//alert(lis.length);
for(var i=0; i<lis.length;i++) {
lis[i].onmouseover = function() {
// alert(this.offsetLeft);
target = this.offsetLeft; // 把左側(cè)的位置 钱雷,給 target
}
lis[i].onmouseout = function() {
target = current; // 鼠標(biāo)離開 target 是 剛才我們點(diǎn)擊的位置
}
lis[i].onclick = function() {
current = this.offsetLeft; // 點(diǎn)擊的時(shí)候 吧當(dāng)前位置 存貯一下
}
}
// 緩動(dòng)公式
var leader = 0, target = 0;
setInterval(function(){
leader = leader + (target - leader ) / 10;
cloud.style.left = leader + "px";
},10);
</script>
特效二:鼠標(biāo)點(diǎn)擊跟隨
效果:
<!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;
}
// 緩動(dòng)
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>
特效二:鼠標(biāo)放大鏡
效果:
<!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() { // 鼠標(biāo)經(jīng)過顯示出他們
mask.style.display = "block";
big.style.display = "block";
}
small.onmouseout = function() {
mask.style.display = "none";
big.style.display = "none";
}
// 鼠標(biāo)在small 內(nèi)移動(dòng)
var x = 0;
var y = 0;
small.onmousemove = function(event) {
var event = event || window.event;
x = event.clientX - this.offsetParent.offsetLeft - mask.offsetWidth /2; // 再某個(gè)盒子內(nèi)的坐標(biāo)
//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";
/*計(jì)算 : 夫子 一頓吃 2個(gè)饅頭 嬌子 一頓 4個(gè)饅頭
問 夫子今天吃了 3個(gè)饅頭 嬌子應(yīng)該吃幾個(gè)? */
/*計(jì)算出他們的倍數(shù) 4 / 2 2倍
3 * 2 == 6個(gè) */
/* 大圖盒子 / 小圖盒子 倍數(shù)
我們 再小圖移動(dòng)的距離 * 倍數(shù) == 大圖的位置*/
bigImage.style.left = -x * big.offsetWidth /small.offsetWidth + "px";
bigImage.style.top = -y * big.offsetHeight /small.offsetHeight + "px";
}
</script>