元素绝对位置;鼠标移入移出;input框事件;jQuery其他事件

元素绝对位置

尺寸相关、滚动事件

1、获取和设置元素的尺寸

width()、height()    获取元素width和height 

innerWidth()、innerHeight()  包括padding的width和height 

outerWidth()、outerHeight()  包括padding和border的width和height 

outerWidth(true)、outerHeight(true)  包括padding和border以及margin的width和height

2、获取元素相对页面的绝对位置

offset()

3、获取可视区高度

$(window).height();

4、获取页面高度

$(document).height();

5、获取页面滚动距离

$(document).scrollTop(); 

$(document).scrollLeft();

6、页面滚动事件

$(window).scroll(function(){ 

    ...... 

})


代码:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>元素绝对位置</title>

<style type="text/css">

.con{

width: 600px;

height: 600px;

margin: 50px auto 0;

}

.box{

width: 100px;

height: 100px;

background-color: gold;

margin-bottom: 10px;

}

.pos{

margin-left: 500px;

}

.pop{

width: 100px;

height: 100px;

background-color: red;

position: fixed;

left:0;

top: 0;

display: none;

}

</style>

<script type="text/javascript" src="JS/jquery-1.12.4.min.js"></script>

<script type="text/javascript">

$(function(){

var $pos = $('.pos');

//offset()是获取相对于页面左上角的绝对位置,即使外面再包一层con居中层,也不影响效果

var pos = $pos.offset();

// console.log(pos);

// alert(pos.left + "," + pos.top);

var w = $pos.outerWidth();

var h = $pos.outerHeight();

// alert(w);

$('.pop').css({left:pos.left + w,top:pos.top});

$pos.mouseover(function() {

$('.pop').show();

});

$pos.mouseout(function() {

$('.pop').hide();

});

})

</script>

</head>

<body>

<div class="con">

<div class="box"></div>

<div class="box"></div>

<div class="box pos"></div>

<div class="box"></div>

</div>

<div class="pop">提示信息!</div>

</body>

</html>


鼠标移入移出

jquery事件

事件函数列表:

mouseover() 鼠标进入(进入子元素也触发)

mouseout() 鼠标离开(离开子元素也触发)

mouseenter() 鼠标进入(进入子元素不触发)

mouseleave() 鼠标离开(离开子元素不触发)

hover() 同时为mouseenter和mouseleave事件指定处理函数


代码:

<!DOCTYPE html>

<html lang="en">

  <meta charset="UTF-8">

  <title>鼠标移入移出

  <style type="text/css">

      .box{

width:200px;

        height:200px;

        background-color:gold;

        margin:100px auto 0;

      }

.son{

width:100px;

        height:100px;

        background-color:green;

      }

  <script type="text/javascript" src="JS/jquery-1.12.4.min.js">

  <script type="text/javascript">

      $(function(){

/*进入子元素也触发*/

/*$('#div1').mouseover(function() {

$(this).animate({marginTop: 50});//.stop()

});

$('#div1').mouseout(function() {

$(this).animate({marginTop: 100});//.stop()

});*/

        /*进入子元素不触发*/

/*$('#div1').mouseenter(function() {

$(this).stop().animate({marginTop: 50});//

});

$('#div1').mouseleave(function() {

$(this).stop().animate({marginTop: 100});//

});*/

        /*通过hover(mouseenter+mouseleave)实现简写*/

        $('#div1').hover(function() {

$(this).stop().animate({marginTop:50});

        }, function() {

$(this).stop().animate({marginTop:100});

        });

      })

  <div id="div1" class="box">

      <div class="son">

</html>



input框事件

blur() 元素失去焦点

focus() 元素获得焦点

change() 表单元素的值发生变化

click() 鼠标单击

keydown() 按下键盘

keypress() 按下键盘

keyup() 松开键盘

(不常用)设计手机网页:mouseup() 松开鼠标

mousedown() 按下鼠标

mousemove() 鼠标在元素内部移动


代码:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>input框事件</title>

<style type="text/css">

</style>

<script type="text/javascript" src="JS/jquery-1.12.4.min.js"></script>

<script type="text/javascript">

$(function(){

// //一开始就获取焦点,相当于设置了autofocus自动获取焦点了(HTML5 新增表单控件属性)

// $('#txt01').focus();

// //文本框获取焦点的时候(有光标闪烁的时候)

// $('#txt01').focus(function() {

// alert('focus');

// });

// //失去焦点的时候(光标离开的时候)

// $('#txt01').blur(function() {

// alert('blur');

// });

// //输入框内容发生变化的时候,失去焦点后触发,可用于注册时验证用户名是否已存在

// $('#txt01').change(function() {

// alert('值变了');

// });

//松开键盘按键就触发

$('#txt01').keyup(function() {

alert('松开键盘了');

});

})

/*原生js写法

window.onload = function () {

}*/

/*$(window).load(function(){

})

$(document).ready(function () {

})

$(function () {

})*/

$(window).resize(function () {

console.log('窗口尺寸变化了')

})

</script>

</head>

<body>

<input type="text" id="txt01">

</body>

</html>



jQuery其他事件

load() 元素加载完毕

ready() DOM加载完成

resize() 浏览器窗口的大小发生改变

scroll() 滚动条的位置发生变化

submit() 用户递交表单(常用)

toggle() 根据鼠标点击的次数,依次运行多个函数

unload() 用户离开页面 (看小说保存进度,了解)


代码:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>jQuery其他事件</title>

<style type="text/css">

</style>

<script type="text/javascript" src="JS/jquery-1.12.4.min.js"></script>

<script type="text/javascript">

/*原生js写法

window.onload = function () {

}*/

/*$(window).load(function(){

})

$(document).ready(function () {

})

$(function () {

})ready的简写*/

$(window).resize(function () {

console.log('窗口尺寸变化了')

})

</script>

</head>

<body>

<div id="div1"></div>

</body>

</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容