尺寸相关、滚动事件
1、获取和设置元素的尺寸
width()、height() 获取元素的width和height
innerWidth()、innerHeight() 包括padding的width和height
outerWidth()、outerHeight() 包括padding和border的width和height
outerWidth(true)、outerHeight(true) 包括padding和border以及margin的margin的width和height
<!DOCTYPE html>
<html>
<head>
<title>jquery</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
var $div = $('.box');
// 盒子内容的尺寸
console.log($div.width());
console.log($div.height());
// 盒子内容+padding的尺寸
console.log($div.innerWidth());
console.log($div.innerHeight());
// 盒子的真实尺寸:内容+padding+border
console.log($div.outerWidth());
console.log($div.outerHeight());
// 盒子的真实尺寸+margin
console.log($div.outerWidth(true));
console.log($div.outerHeight(true));
});
</script>
<style type="text/css">
.box {
width: 300px;
height: 200px;
padding: 20px;
border: 10px solid #000;
margin: 20px;
background-color: gold;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
加入购物车demo
<!DOCTYPE html>
<html>
<head>
<title>jquery</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
var $chart = $('.chart');
var $count = $('.chart em');
var $btn = $('.add');
var $point = $('.point');
var $w01 = $btn.outerWidth();
var $h01 = $btn.outerHeight();
var $w02 = $chart.outerWidth();
var $h02 = $chart.outerHeight();
$btn.click(function(){
var oPos01 = $btn.offset();
var oPos02 = $chart.offset();
$point.css({'left':oPos01.left + parseInt($w01/2) - 8,'top':oPos01.top + parseInt($h01/2) - 8});
$point.show();
$point.animate({'left':oPos02.left + parseInt($w02/2) - 8,'top':oPos02.top + parseInt($h02/2) - 8},800,function() {
$point.hide();
var iNum = $count.html();
$count.html(parseInt(iNum) + 1);
});
})
});
</script>
<style type="text/css">
.chart {
width: 150px;
height: 50px;
border: 2px solid #000;
text-align: center;
line-height: 50px;
float: right;
margin-right: 100px;
margin-top: 50px;
}
.chart em {
font-style: normal;
color: red;
font-weight: bold;
}
.add {
width: 100px;
height: 50px;
border: 0;
background-color: green;
color: #fff;
float: left;
margin-top: 300px;
margin-left: 300px;
}
.point {
width: 16px;
height: 16px;
background-color: red;
position: fixed;
left: 0;
top: 0;
display: none;
z-index: 9999;
}
</style>
</head>
<body>
<div class="chart">购物车<em>0</em></div>
<input type="button" name="" value="加入购物车" class="add">
<div class="point"></div>
</body>
</html>
2、获取元素相对页面的绝对位置
offset()
3、获取浏览器可视区宽度高度
$(window).width();
$(window).height();
4、获取页面文档的宽度高度
$(document).width();
$(document).height();
5、获取页面滚动距离
$(document).scrollTop();
$(document).scrollLeft();
6、页面滚动事件
$(window).scroll(function() {
...
});
<!DOCTYPE html>
<html>
<head>
<title>js</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
console.log('可视区的宽度:'+ $(window).width());
console.log('可视区的高度:'+ $(window).height());
console.log('文档的宽度:'+ $(document).width());
console.log('文档的宽度:'+ $(document).height());
});
</script>
</head>
<body>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
</body>
</html>
悬浮菜单、滚动到顶部
<!DOCTYPE html>
<html>
<head>
<title>js</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
console.log('可视区的宽度:'+ $(window).width());
console.log('可视区的高度:'+ $(window).height());
console.log('文档的宽度:'+ $(document).width());
console.log('文档的宽度:'+ $(document).height());
});
</script>
<style type="text/css">
.banner {
width: 960px;
height: 200px;
background-color: cyan;
margin: 0 auto;
}
.menu {
width: 960px;
height: 80px;
background-color: gold;
margin: 0 auto;
text-align: center;
line-height: 80px;
}
.menu_back {
width: 960px;
height: 80px;
margin: 0 auto;
display: none;
}
p {
text-align: center;
}
.totop {
width: 60px;
height: 60px;
background-color: #000;
color: #fff;
position: fixed;
right: 20px;
bottom: 50px;
line-height: 60px;
text-align: center;
font-size: 40px;
border-radius: 50%;
cursor: pointer;
display: none;
}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
var $menu = $('.menu');
var $menu_back = $('.menu_back');
var $totop = $('.totop');
$(window).scroll(function() {
var iNum = $(document).scrollTop();
if (iNum > 200) {
$menu.css({
'position':'fixed',
'left':'50%',
'top':0,
'marginLeft':-480,
});
$menu_back.show();
} else {
$menu.css({
'position':'static',
'marginLeft':'auto'
})
$menu_back.hide();
}
if (iNum > 400) {
$totop.fadeIn();
} else {
$totop.fadeOut();
}
});
$totop.click(function() {
$('html,body').animate({'scrollTop':0});
})
});
</script>
</head
<body>
<div class="banner"></div>
<div class="menu">菜单</div>
<div class="menu_back"></div>
<div class="totop">↑</div>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
<p>文档内容</p>
<br>
<br>
<br>
<br>
<br>
</body>
</html>
jquery属性操作
1、html()取出或这是html内容
// 取出html内容
var $htm = $('#div ').html();
// 设置html内容
$('#div1 ').html('<span>添加文字</span>');
2、prop()取出或设置某个属性的值
// 取出图片的地址
var $src = $('#img ').prop('src');
// 设置图片的地址和alt属性
$('#img1 ').prop({src:"text.jpg","alt":"Test Image"});
<!DOCTYPE html>
<html>
<head>
<title>js</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
var $a = $('.link');
var $img = $('#img01');
var $div = $('#div1');
// 读取属性值
console.log($a.prop('class'));
// 没有设置的属性读取为空
console.log($a.prop('title'));
// 读取的图片的绝对的地址
console.log($img.prop('src'));
// 设置属性值
$a.prop({'href':'http://www.baidu.com','title':'百度地址'});
// 读取标签内包裹的内容
console.log($a.html());
$div.html('<span>div里面的span标签</span>')
})
</script>
</head>
<body>
<a href="#" class="link">这是一个链接</a>
<img src="images/002.jpg" id="img01" alt="水果">
<div id="div1"></div>
</body>
</html>
手风琴效果
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<style>
*{margin:0;padding:0;}
body{font-size:12px;}
#accordion{width:727px; height:350px; margin:100px auto 0 auto; position:relative; overflow:hidden; border:1px solid #CCC;}
#accordion ul{list-style:none;}
#accordion ul li{width:643px;height:350px; position:absolute; background:#FFF;}
#accordion ul li span{display:block;width:20px; height:350px; float:left; text-align:center; color:#FFF; padding-top:5px; cursor:pointer;}
#accordion ul li img{display:block; float:right;}
.bar01{left:0px;}
.bar02{left:643px;}
.bar03{left:664px;}
.bar04{left:685px;}
.bar05{left:706px;}
.bar01 span{background:#09E0B5;}
.bar02 span{background:#3D7FBB;}
.bar03 span{background:#5CA716;}
.bar04 span{background:#F28B24;}
.bar05 span{background:#7C0070;}
</style>
<script type="text/javascript">
$(function(){
$('#accordion li').click(function() {
$(this).animate({left:$(this).index()*21});
$(this).prevAll().each(function(){
$(this).animate({left:$(this).index()*21});
});
$(this).nextAll().each(function(){
$(this).animate({left:(727-(5-$(this).index())*21)});
});
});
})
</script>
<title>手风琴效果</title>
</head>
<body>
<div id="accordion">
<ul>
<li class="bar01"><span>非洲景色01</span><img src="images/001.jpg" /></li>
<li class="bar02"><span>非洲景色02</span><img src="images/002.jpg" /></li>
<li class="bar03"><span>非洲景色03</span><img src="images/003.jpg" /></li>
<li class="bar04"><span>非洲景色04</span><img src="images/004.jpg" /></li>
<li class="bar05"><span>非洲景色05</span><img src="images/005.jpg" /></li>
</ul>
</div>
</body>
</html>
jquery循环
对jQuery选择的对象集合分别进行操作,需要用到juqery循环操作,此时可以用对象的each方法:
<!DOCTYPE html>
<html>
<head>
<title>jquery循环</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
var $li = $('.list li');
$li.each(function() {
//alert($(this).html())
if ($(this).index() % 2 == 0) {
$(this).css({"background": "gold"});
}
})
})
</script>
</head>
<body>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
</html>
网友评论