jq第4版第4章

html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
     <link rel="stylesheet" href="4.css" type="text/css" />
    <script  language="JavaScript" src="index.js" charset="UTF-8"></script>
    <script  language="JavaScript" src="jquery.js" charset="UTF-8"></script>
</head>
<body>
<div id="container">
      <h2>Abraham Lincoln‘s Gettysburg Address</h2>
      <div id="switcher">
        <div class="label">Text Size</div>
        <button id="switcher-default">Default</button>
        <button id="switcher-large">Bigger</button>
        <button id="switcher-small">Smaller</button>
      </div>
      <div class="speech">
        <p>Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
        <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground.</p>
        <a href="#" class="more">read more</a>
        <p>The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.</p>
        <p>It is rather for us to be here dedicated to the great task remaining before us&mdash;that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion&mdash;that we here highly resolve that these dead shall not have died in vain&mdash;that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.</p>
      </div>
    </div>
</body>
</html>

css

html, body { margin: 0;   padding: 0; }

body {   font: 62.5% Verdana, Helvetica, Arial, sans-serif;   color: #000;   background: #fff; } #container {   font-size: 1.2em;   margin: 10px 2em; }

h1 {   font-size: 2.5em;   margin-bottom: 0; }

h2 {   font-size: 1.3em;   margin-bottom: .5em; } h3 {   font-size: 1.1em;   margin-bottom: 0; }

code {   font-size: 1.2em; }

a {   color: #06581f; }

/***************************************    Chapter Styles ************************************** */

#switcher {   width: 300px;   padding: .5em;   border: 1px solid #777; } .label {   width: 130px;   margin: .5em 0;   cursor: pointer; }

jq/js

$(document).ready(function(){    $(‘#switcher-large‘).click(function(){        alert(‘OK‘)    }) });

//修改内联css:首先在单独样式表里面定义好样式,然后再通过jq来添加或者删除这些类 //jq提供了一个方法.css():获取匹配元素集合中的第一个元素的样式属性的计算值  或  设置每个匹

配元素的一个或多个CSS属性。 //.css()语法 //取得单个元素.css(objattribute) //取得多个.css([‘obj1‘,‘obj2‘])返回{‘obj1‘:‘obj1.1‘,‘obj2‘:‘obj2.2‘} //.css()可能接受2种参数,单个属性及其值.css(‘obj1‘,‘obj1.1‘) //.css()键值对.css({obj1:‘obj1.1‘,‘obj2‘:‘obj2.2‘}) //.css()方式与使用.addClass()方式一样不 //设置计算样式属性值 //div.speech可以获取当前字体大小,如下 //$(document).ready(function(){ //    var $speech=$(‘div.speech‘); //   $(‘#switcher-large‘).click(function(){ //       var num=parseFloat($speech.css(‘fontSize‘))//获取小数部分信息 //        num*=1.4; //       $speech.css(‘fontSize‘,num+‘px‘); //   }) //}); //注意:parseFloat()函数会在一个字符串中从左到右查找一个浮点(十进制)数 //对字体进行缩小方式是num/=1.4 $(document).ready(function(){     var $speech=$(‘div.speech‘);     var num=parseFloat($speech.css(‘fontSize‘));      if(this.id==‘switcher-large‘){          num*=1.4;          //这边的this引用的dom元素id属性,如果只需要测试属性值,this比创建jq对象更有效      }else if(this.id==‘switcher-small‘){          num/=1.4      }     $speech.css(‘fontSize‘,num+‘px‘) });

$(document).ready(function(){     var $speech=$(‘div.speech‘);     var num=parseFloat($speech.css(‘fontSize‘));     var defaultSize=$speech.css(‘fontSize‘);      $(‘#switcher button‘).click(function(){         var num=parseFloat($speech.css(‘fontSize‘))          switch (this.id){              case  ‘switcher-large‘:                  num*=1.4;                  break;              case  ‘switcher-small‘:                  num/=1.4;                  break;              default :                  num=parseFloat(defaultSize);          }          $speech.css(‘fontSize‘,num+‘px‘)      });     //这些继续使用this.id的值以改变字体大小,它即不是switcher-large也不是switcher-small,那

么就是默认初始字体大小

}); //带厂商前缀样式属性,使用以下css声明,如下 /* * -webkil-property-name:value * -moz-property-name:value * -ms-property-name:value * property-name:value*/ //如果上面的这些属性需要测试它们在dom是否存在,从propertName到webkitpropertyName再到

mspropertyName都检查,但在jq中 //.css(‘propertyName‘,‘value‘),jq就会依次检查所有前缀的(webkit,o,moz,ms)属性,然后使用第一

个找到那个属性

//隐藏和显示元素 //.hide()和.show()方法不带任何参数 //.hide()方法会将匹配元素集合的内联style属性的display:none //block通常是display的属性之一,inline或者inline-block, //display属性相关知识:https://developer.mozilla.org/en-US/docs/Web/CSS/display //或者https://developer.mozilla.org/samples/cssref/display.html //想隐藏或者显示元素,如下(例子)eq(1).show() $(document).ready(function(){     $(‘p‘).eq(1).hide();     $(‘a.move‘).click(function(e){         e.preventDefault();//避免链接被默认行为         $(‘p‘).eq(1).show();         $(this).hide();         alert(11)     }) });

效果和时长

当在.show()或者.hide()中指定时间(或者更准确地说,一个速度)参数时,就会产生动画效果,即效果会在一个特定的时间段发生,如.hide(‘duration‘)方法,会同时减少元素高度,宽度和不透明度,直到这3个属性值都达到0,.show(‘duration‘)方法是从上往下增大元素从左到右增大元素的宽度,从0到1增加元素不透明度,直到内容完全可见

指定显示速度:slow和fast

.show(‘slow‘)会在600毫秒(.6秒),.show(‘falt‘)则是200毫秒(.2秒),也可以自己输入需要的数值,如.show(850),与字符串表示速度参数名称不同,数值不需要使用引号,如下代码

$(document).ready(function(){
   $(‘p‘).eq(1).hide();
    $(‘a.more‘).click(function(e){
        e.preventDefault();
        $(‘p‘).eq(1).show(‘slow‘);
        $(this).hide()
    })
});

淡入淡出:.fadeIn(‘slow‘)如果想在显示整个段落时,使用此方法

$(document).ready(function(){
   $(‘p‘).eq(1).hide();
    $(‘a.more‘).click(function(e){
        e.preventDefault();
        $(‘p‘).eq(1).fadeIn(‘slow‘);
        $(this).hide()
    })
});

.fadeOut()它可以逐渐减少不透明度

滑上和滑下

.slideDown(‘slow‘),
.slideUp()
这2个动画方法仅改变元素的高度$(document).ready(function(){   $(‘p‘).eq(1).hide();    $(‘a.more‘).click(function(e){        e.preventDefault();        $(‘p‘).eq(1).slideDown(‘slow‘),        $(this).hide()    })});切换可见性

$(document).ready(function(){

var $firstPara=$(‘p‘).eq(1);

$firstPara.hide();

$(‘a.more‘).click(function(e){

e.preventDefault();

if($firstPara.is(‘:hidden‘)){

$firstPara.fadeIn(‘slow‘);

$(this).text(‘read less‘);

}

else{

$firstPara.fadeOut(‘slow‘);

$(this).text(‘read more‘)

}})

});

//.toggle()方法也可以,另一个复合方法.slideToggle()该方法通过逐渐添加或者减少高度来显示或

者隐藏元素,如下

$(document).ready(function(){

var $firstPara=$(‘p‘).eq(1);

$firstPara.hide();

$(‘a.more‘).click(function(e){

e.preventDefault();

if($firstPara.is(‘:hidden‘)) {

$firstPara.slideToggle(‘slow‘);

var $link=$(this);

if($link.text()==‘read mode‘){

$link.text(‘read less‘)

}else{

$link.text(‘read more‘)

}

}

})

});

创建自定义动画

.animate()方法:用于创建控制更加精细的自定义动画,2种形式,如下:第1种接收以下4个参数.

<1>:一个包含样式属性及值的对象,与.css()方法类似

<2>:可选的时长参数可以是预置的字符串,也可以是毫秒数值

<3>:可选的缓动(easing)类型:(第11章学习)

<4>:可选的回调函数

例如

.animate({k1:v1,k2:v2},duration(持续),easing,function(){

    return ‘动画‘})

//第2种接受两个参数:一个是属性对象和一个选项对象

如下

.animate({properties},{options})

.animate({k:v1,k2:v2},{duration:v,easing:v,specialEasing:{

    p1:easing1,p2:‘easing2‘},complete:function(){return},queue:true,step:callback});

手式创建效果

$(document).ready(function(){

var $firstPara=$(‘p‘).eq(1);

$firstPara.hide();

$(‘a.more‘).click(function(e){

e.preventDefault();

$firstPara.animate({height:‘toggle‘},‘slow‘);

var $link=$(this);

if($link.text()==‘read mode‘){

$link.text(‘read less‘)

}else{

$link.text(‘read more‘)

}

});

});

一次给多个属性添加动画效果

$(document).ready(function(){

var $firstPara=$(‘p‘).eq(1);

$firstPara.hide();

$(‘a.more‘).click(function(e){

e.preventDefault();

$firstPara.animate({

opacity:‘toggle‘,

height:‘toolg‘

},‘slow‘);

var $link=$(this);

if($link.text()==‘read mode‘){

$link.text(‘read less‘)

}else{

$link.text(‘read more‘)

}

});

});

//通过这样的方法,可以将css里面的属性换成.animate()来执行(复杂的动画效果也是可以的,如下

$(document).ready(function(){

var $speech=$(‘div.speech‘);

var num=parseFloat($speech.css(‘fontSize‘));

var defaultSize=$speech.css(‘fontSize‘);

$(‘#switcher button‘).click(function(){

var num=parseFloat($speech.css(‘fontSize‘));

switch (this.id){

case  ‘switcher-large‘:

num*=1.4;

break;

case  ‘switcher-small‘:

num/=1.4;

break;

default :

num=parseFloat(defaultSize);

}

$speech.animate({‘fontSize‘:num+‘px‘},‘slow‘)

});

});

jq中有outWidth()方法可以用于计算宽度,包括内边距及边框宽度,如下

//通过这样的方法,可以将css里面的属性换成.animate()来执行(复杂的动画效果也是可以的,如下

$(document).ready(function(){

$(‘div.lable‘).click(function(){

var paraWidget=$(‘div.speech p‘).outerWidth();

var $switcher=$(this).parent();

var switcherWidget=$switcher.outerWidth();

$switcher.animate({

boredeWidth:‘5px‘,left:paraWidget-switcherWidget,

height:‘+=20px‘

},‘slow‘)

})

});

//boredeWidth给它指定一个常用的值加个单位,和样式表中很类似left是用于计算数值的,它后面的

是可选的,如果不指定,就会默认以px作为单位

//+=操作符表示相对值

//通过css来定位,如下

$(document).ready(function(){

$(‘div.lable‘).click(function(){

var paraWidget=$(‘div.speech p‘).outerWidth();

var $switcher=$(this).parent();

var switcherWidget=$switcher.outerWidth();

$switcher.css({position:"relative‘"}).animate({

boredeWidth:‘5px‘,left:paraWidget-switcherWidget,

height:‘+=20px‘

},‘slow‘)

})

});

时间: 2024-08-29 14:10:25

jq第4版第4章的相关文章

Java 线程第三版 第四章 Thread Notification 读书笔记

一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(long timeout) throws InterruptedException 等待条件的发生.如果通知没有在timeout指定的时间内发生,它还是会返回. public final void wait(long timeout, int nanos) throws InterruptedException

C++ Primer Plus 第六版 第16章 string类和标准模板库

1.string实际上是模板具体化basic_string<char> 的一个typedef,有默认参数,所以省略了初始化参数 2.size_type是一个依赖于实现的整形 string将string::npos定义为字符串的最大长度 3.string类的构造函数P656 4.对于c-风格字符串,3种输入方法:cin>>   cin.getline(),cin.get 对于string   ,2种输入方法:cin>>,getline(cin,string对象) 5.st

Java 线程第三版 第六章 高级同步议题 读书笔记

多线程数据同步错误比较难检测,因为通常是与事件的特定发生顺序有关. 一.同步术语 Barrier(屏障) barrier是多个Thread的集合点:所有的Thread都应该到齐在这个barrier之后才能允许它们继续下去. Condition variable(条件变量) 实际上不是变量,而是与某个lock有关联的变量. Event variable(事件变量) 条件变量的另一个名称. Critical section(临界区) 临界区是synchronized方法或者block. Lock(锁

c++ primer plus(第6版)中文版 第九章编程练习答案

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

Java 线程第三版 第三章数据同步 读书笔记

多线程间共享数据问题 一.Synchronized关键字 atomic一词与"原子"无关,它曾经被认为是物质的最小的单元,不能再被拆解成更小的部分. 当一个方法被声明成synchronized,要执行此方法的thread必须先取得一个token,我们将它称为锁.一旦该方法取得(或者说是获得)锁,它将运行此方法然后释放掉(或者返回)此锁.不管方法时怎样返回的(包括通过异常)该锁会被释放. 二.Volatile关键字 如果变量被标示为volatile,每次使用该变量时都必须从主寄存器中读出

Java 线程第三版 第五章 极简同步技巧 读书笔记

一.能避免同步吗? 取得锁会因为以下原因导致成本很高: 取得由竞争的锁需要在虚拟机的层面上运行更多的程序代码. 要取得有竞争锁的线程总是必须等到锁被释放后. 1. 寄存器的效应 计算机有一定数量的主寄存器用来存储与程序有关的数据. 从逻辑上的观点来看,每个Thread都有自己的一组寄存器.当操作系统将某个Thread分配给CPU时,它会把该Thread特有的信息加载到CPU的寄存器中.在分配不同的Thread给CPU之前,它会将寄存器的信息存下来.所以Thread间绝不会共享保存在寄存器的数据.

Python核心编程(第二版) 第二章习题答案 未完待续

2-2.程序输出.阅读下面的Python脚本.#!/usr/bin/env python1 + 2 * 4(a)你认为这段脚本是用来做什么的?(b)你认为这段脚本会输出什么?(c)输入以上代码,并保存为脚本,然后运行它,它所做的与你的预期一样吗?为什么一样/不一样?(d)这段代码单独执行和在交互解释器中执行有何不同?试一下,然后写出结果.(e)如何改进这个脚本,以便它能和你想象的一样工作?答:(a)这段脚本是用来计算表达式的值(b)脚本会输出9(c)保存为脚本,运行后没有输出.和自己预期不一样.

《C++ Primer》 第四版 第7章 函数

<C++ Primer> 第四版 第7章 函数 思维导图笔记 超级具体.很具体,图片版,有利于复习查看 http://download.csdn.net/detail/onlyshi/9479711

《Python核心编程》第二版第五章答案

5-1.整型.讲讲Python普通整型和长整型的区别. Python的标准整形类型是最通用的数字类型.在大多数32位机器上,标准整形类型的取值范围是-2**32-2**32 - 1. Python的长整型类型能表达的数值仅仅与你的机器支持的(虚拟)内存大小有关,换句话说,Python能轻松表达很大的整数. 长整型类型是标准整形类型的超集,当程序需要使用比标准整形更大的整型时,可以使用长整型类型,在整型值后面添加L,表示这个为长整型,这两种整形类型正在逐渐统一为 一种. 5-2.操作符.(a)写一