Day-40

jQuery效果与事件jQueryHTML与插件

1、jQuery css() 方法

(1)、jQuery css() 方法是什么

css() 方法设置或返回被选元素的一个或多个样式属性。

(2)、返回 CSS 属性

如需返回指定的 CSS 属性的值,请使用如下语法:

css("propertyname");

$("p").css("background-color");

(3)、设置CSS属性

如需设置指定的 CSS 属性,请使用如下语法:

css("propertyname","value");

$("p").css("background-color","yellow");

(4)、设置多个CSS属性

如需设置多个 CSS 属性,请使用如下语法:

css({"propertyname":"value","propertyname":"value",...});

$("p").css({"background-color":"yellow","font-size":"200%"});

演示示例:jQuery CSS方法

课堂练习:元素的宽、高、文字颜色、背景图发生变化

2、jQuery css类

  1. 、addClass()

向被选元素添加一个或多个类

.important { font-weight:bold; font-size:xx-large; }

.blue { color:blue; }

$("button").click(function(){

$("h1,p").addClass("blue");

    $("div").addClass("important");

});

  1. 、removeClass()

 从被选元素删除一个或多个类

$("button").click(function(){ $("h1,h2,p").removeClass("blue"); });

  1. 、toggleClass()

对被选元素进行添加/删除类的切换操作

$("button").click(function(){ $("h1,h2,p").toggleClass("blue"); });

  1. 、eq()

 方法返回带有被选元素的指定索引号的元素,索引号从 0 开头,所以第一个元素的索引号是 0(不是 1)。

$(selector).eq(index)

  1. 、index()

 index() 方法返回指定元素相对于其他同级元素的 index 位置。

$("li").click(function(){

  alert($(this).index());

});

(6)、not()

 Not() 方法返回指定元素之外的元素。

$("input").not(".in1")

演示示例:jQuery CSS类

课堂练习:返回第二个li标签,并使其变色

3、jQuery动画

  1. 、jQuery隐藏显示

可以使用 hide() show() 方法来隐藏和显示 HTML 元素。

$("#hide").click(function(){

$("p").hide();

});

$("#show").click(function(){

$("p").show();

});

可以使用 toggle() 方法来切换 hide() show() 方法。

$("button").click(function(){ $("p").toggle(); });

  1. 、jQuery淡入淡出

fadeIn() 用于淡入已隐藏的元素, fadeOut() 方法用于淡出可见元素。

$("button").click(function(){

  $("#div1").fadeIn();

$("#div2").fadeIn("slow");

$("#div3").fadeOut(3000);

});

 fadeToggle() 方法可以在 fadeIn() fadeOut() 方法之间进行切换。

$("button").click(function(){ ("#div2").fadeToggle("fast");});

 fadeTo() 方法允许渐变为给定的不透明度(值介于 0 1 之间)。

$("button").click(function(){ $("#div1").fadeTo("slow",0.15);});

  1. 、jQuery滑动

 slideDown() 方法用于向下滑动元素, slideUp() 方法用于向上滑动元素。

$("#flip").click(function(){ $("#div1").slideDown(); $("#div1").slideUp();});

 slideToggle() 方法可以在 slideDown() slideUp() 方法之间进行切换。

$("#flip").click(function(){ $("#panel").slideToggle(); });

  1. 、jQuery自定义动画

animate() 方法用于创建自定义动画。可选的 speed 参数规定效果的时长。它可以取以下值:"slow""fast" 或毫秒。可选的 callback 参数是动画完成后所执行的函数名称。

$("button").click(function(){

$("div").animate({

left:'250px', opacity:'0.5', height:'150px', width:'150px'

 }); });

  1. 、stop方法

jQuery stop() 方法用于停止动画或效果,在它们完成之前,适用于所有 jQuery 效果函数,包括滑动、淡入淡出和自定义动画。

$("#stop").click(function(){ $("#panel").stop(); });

  1. 、回调函数

         在当前动画 100% 完成之后执行。

$("button").click(function(){

$("p").hide("slow",function(){

alert("段落现在被隐藏了");

}); });

演示示例:jQuery CSS动画

课堂练习:按钮演示全部动画.

4、jQuery 事件机制

  1. 、注册事件

bind() on() 方法向被选元素添加一个或多个事件处理程序,以及当事件发生时运行的函数。

$("p").bind("click",function(){
         alert("
这个段落被点击了。");
}"dbClick",function(){
         alert("这个段落被双击了。");
});

 $("p").on("click",function(){ alert("段落被点击了。"); });

  1. 、委托事件

delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数

$("div").delegate("p","click",function(){
    $("p").css("background-color","pink");
});

  1. 、事件对象event

event对象有以下属性

type:事件类型,比如click

which:触发该事件的鼠标按钮或键盘的键。

target:事件发生的初始对象。

data:传入事件对象的数据。

pageX:事件发生时,鼠标位置的水平坐标(相对于页面左上角)。

pageY:事件发生时,鼠标位置的垂直坐标(相对于页面左上角

$("button").click(function(event){

          console.log(evet);

    });

  1. 、each()方法

each() 方法为每个匹配元素规定要运行的函数。

$("button").click(function(){

$("li").each(function(){

alert($(this).text())

});  

});

  1. jQuery 对HTML的设置与捕获

jQuery 中非常重要的部分,就是操作 DOM 的能力。

jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易。

  1. 、html()

html() - 设置或返回所选元素的内容(包括 HTML 标记)。

$("#btn2").click(function(){

 alert("HTML: " + $("#test").html());

});

$("#btn2").click(function(){

$("#test2").html("<b>Hello world!</b>");

});

  1. 、text()

text() - 设置或返回所选元素的文本内容

$("#btn1").click(function(){

alert("Text: " + $("#test").text());

});

$("#btn1").click(function(){

$("#test1").text("Hello world!");

});

  1. 、val()

val() - 设置或返回表单字段的值

$("#btn1").click(function(){

alert("值为: " + $("#test").val());

});

$("#btn3").click(function(){

$("#test3").val("RUNOOB");

});

  1. 、text()、html() 以及 val() 的回调函数

上面的三个 jQuery 方法:text()html() 以及 val(),同样拥有回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

$("#btn1").click(function(){

$("#test1").text(function(i,origText){

return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";

});

});

  1. 、attr()、prop()

attr() prop()方法用于获取和返回属性值。

$("button").click(function(){ alert($("#runoob").attr("href")); });

$("button").click(function(){ $("#runoob").attr("href","http://www.runoob.com/jquery"); });

具有 true false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr().attr不仅可以返回(设置)元素的原生属性,还可以返回(设置)自定义属性。

演示示例:jQuery对HTML的设置与捕获

课堂练习:练习示例

  1. jQuery 对HTML的页面尺寸操作  

通过 jQuery,很容易处理元素和浏览器窗口的尺寸。

jQuery 尺寸:

  1. 、width() 和 height() 方法

width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。

height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。

$("button").click(function(){

"div 的宽度是: " + $("#div1").width() + "</br>";

    "div 的高度是: " + $("#div1").height(20);

});

  1. 、innerWidth() 和 innerHeight() 方法

innerWidth() 方法返回元素的宽度(包括内边距)。

innerHeight() 方法返回元素的高度(包括内边距)。

$("button").click(function(){

"div 宽度,包含内边距: " + $("#div1").innerWidth();

"div 高度,包含内边距: " + $("#div1").innerHeight();

});

  1. 、outerWidth() 和 outerHeight() 方法

outerWidth() 方法返回元素的宽度(包括内边距和边框)。

outerHeight() 方法返回元素的高度(包括内边距和边框)。

$("button").click(function(){

txt+="div 宽度,包含内边距和边框: " + $("#div1").outerWidth()

        txt+="div 高度,包含内边距和边框: " + $("#div1").outerHeight();

    });

  1. 、scrollTop() 和 scrollLeft() 方法

scrollTop() 方法设置或者返回滚动条被卷去的元素的高度

scrollLeft() 方法设置或者返回滚动条被卷去的元素的宽度

$("button").click(function(){ alert($("div").scrollTop()); });

演示示例:jQuery对HTML的页面尺寸操作

课堂练习:练习以上示例

  1. jQuery添加元素和删除元素
  1. 、append()方法

 append() 方法在被选元素的结尾插入内容(仍然在该元素的内部)  

$("ol").append("<li>追加列表项</li>");

  1. 、append() 方法

 prepend() 方法在被选元素的开头插入内容。

$("ol").prepend("<li>追加列表项</li>");

  1. 、after() 和 before() 方法

jQuery after() 方法在被选元素之后插入内容。

jQuery before() 方法在被选元素之前插入内容。

$("img").before("<b>之前</b>");

$("img").after("<i>之后</i>");

  1. 、删除元素/内容

remove() - 删除被选元素(及其子元素)

empty() - 从被选元素中删除子元素

empty()仅仅删除元素的文本,占有位置,不显示在页面,但是DOM节点并没有删除

remove()把整个dom节点删除掉,,不占有位置

4、jquery插件的认识

(1)、插件

jquery不可能包含所有的功能,我们可以通过插件扩展jquery的功能。

jquery有着丰富的插件,使用这些插件能给jquery提供一些额外的功能。

5、jquery.color.js的使用

  1. 、引入js文件

jquery中的animate动画本身不支持变色,但是jquery.color.js弥补了这一缺陷。

.color.js 依赖于 jQuery. 所以需要先引用jqueryjs

<script src="jquery.min.js"></script>

<script src="jquery.color.js"></script>

  1. 、示例

$("button").on("click", function () {

                $("div").animate({"width":200,"background-color":"red"},2000,                 function () {

                    alert("动画结束");

                });

            });

演示示例:jquery.color插件

课堂练习:使用animate动画改变颜色

6、jquery.lazyload.js的使用

(1)、引入js文件

很多网站都会用到‘图片懒加载’这种方式对网站进行优化,即延迟加载图片或符合某些条件才开始加载图片。
         懒加载原理:浏览器会自动对页面中的img标签的src属性发送请求并下载图片。通过动态改变imgsrc属性实现。它可以延迟加载长页面中的图片在浏览器可视区域外,图片不会被载入,直到用户将页面滚动到它们所在的位置。
与图片预加载的处理方式相反,在包含很多大图片长页面中延迟加载图片可以加快页面加载速度,尤其是移动端。浏览器将会在加载可见图片之后即进入就绪状态,在淘宝商品详情页、漫画网站很多图片的情况下还可以帮助降低服务器负担。

<script src="jquery.min.js"></script>

<script src="jquery.lazyload.js"></script>

(2)、示例

<img alt="" width="640" height="480" data-original="img/test.jpg" />

$(function() {
        $("img").lazyload();
    });

演示示例:jquery.lazyload插件

课堂练习:图片点击后,开始加载

7、jquery.ui.js的使用

  1. 、jQuery UI是什么?

jQuery UI 是建立在 jQuery JavaScript 库上的一组用户界面交互、特效、小部件及主题。无论您是创建高度交互的 Web 应用程序还是仅仅向窗体控件添加一个日期选择器,jQuery UI 都是一个完美的选择。

jQuery UI 包含了许多维持状态的小部件(Widget),因此,它与典型的 jQuery 插件使用模式略有不同。所有的 jQuery UI 小部件(Widget)使用相同的模式,所以,只要您学会使用其中一个,您就知道如何使用其他的小部件(Widget)。

  1. 、引入

下载zip包并解压,通常情况下,您需要在页面中引用这三个文件,以便使用 jQuery UI 的窗体小部件和交互部件:

          <link rel="stylesheet" href="jquery-ui-1.12.1/jquery-ui.css" />
          <script src="js/jquery-1.11.1.min.js"></script>
          <script src="jquery-ui-1.12.1/jquery-ui.min.js"></script>
  1. 、操作日期选择器

一旦您引用了这些必要的文件,您就能向您的页面添加一些 jQuery 小部件。比如,要制作一个日期选择器(datepicker)小部件,您需要向页面添加一个文本输入框,然后再调用 .datepicker(),如下所示:

          <input type="text" name="date" id="date" />
           $( "#date" ).datepicker();

  1. 拖动

允许鼠标拖动元素,在任意的 DOM 元素上启用 draggable 功能。通过鼠标点击并在视区中拖动来移动 draggable 对象

  $(function() {
    $( "#draggable" ).draggable();
  });

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化下面代码.bg { width: 100%; height: 100vh; background-image: url('../../assets/img/info-bg.png'); background-size: 100% 100%; background-repeat: no-repeat; position: relative; font-family: AlibabaPuHuiTiR; .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #ffffff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #ffffff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #ffffff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background-image: url('../../assets/img/howo.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-zk-img { width: 56%; height: 90%; background-image: url('../../assets/img/heavyT.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-gj-img { width: 82%; height: 90%; background-image: url('../../assets/img/transit.png'); background-size: 100% 100%; background-repeat: no-repeat; } .car-online { margin-bottom: 50px; } } .day-data { display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #ffffff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; }
05-31
Here are some suggestions to optimize the code: 1. Use shorthand properties whenever possible. For example, instead of writing `background-size: 100% 100%;`, you can write `background-size: cover;`. 2. Consolidate similar styles into classes, instead of repeating them for every element. For example, you can create a class for the font family and apply it to all elements that use that font. 3. Remove unnecessary styles that are not being used or overwritten by other styles. 4. Use more specific selectors to target elements, instead of relying on the order of elements in the HTML. This will make the code more robust and easier to maintain. 5. Consider using a CSS preprocessor like Sass or Less, which can help you write cleaner and more organized code. Here's an example of how the code could be optimized: ``` .bg { width: 100%; height: 100vh; background: url('../../assets/img/info-bg.png') no-repeat center center / cover; position: relative; font-family: AlibabaPuHuiTiR; } .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #fff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #fff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #fff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background: url('../../assets/img/howo.png') no-repeat center center / cover; } .model-zk-img { width: 56%; height: 90%; background: url('../../assets/img/heavyT.png') no-repeat center center / cover; } .model-gj-img { width: 82%; height: 90%; background: url('../../assets/img/transit.png') no-repeat center center / cover; } .car-online { margin-bottom: 50px; } .day-data { display: flex; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #fff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #fff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #fff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; } .font-alibaba { font-family: AlibabaPuHuiTiR; } .font-alibaba-bold { font-family: AlibabaPuHuiTiB; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值