Skip to content

jQuery常用动画、ajax、事件 #50

Open

Description

@Yangxiaqing

Ajax:

    $.ajax({
        url:"json/test.json?_="+Math.random(),
        type:"get",
        dataType:"json",//"text"、"html"、"jsonp"...
        success: function(data){
            concole.log(data);
        }
    });

事件:

语法:

	$("#box").on("click", function () {
		
	});
例:
	var $box = $("#box");
	$box.on("click", function () {
		console.log(1);//this:当前元素(JS原生对象) $(this)
	});
	$box.on("click", function () {
		console.log(2);
	});
	$box.on("mouseover", function () {
		console.log(3);
	});
	$box.on("mouseout", function () {
		console.log(4);
	});

工具:

  • jQuery.browser.version——略
  • each——在jQuery中有两个each
    • 循环选择器获取到的jQuery集合中的每一项
	$("div").each(function (index, item) {
		console.log(item);
	});
+ 循环数组、类数组、对象中的每一项
	$.each([12,13,23], function (index, item) {
		console.log(item);
	});
	$.each(document.getElementsByTagName("*"), function (index, item) {
		console.log(item);
	});
	$.each({name: "yang", age: 7}, function (index, item) {
		console.log(item);
	});
  • $.makeArray——等价于listToArray
  • map——在jQuery中也有两个map,和each的原理一样,只是可以支持返回值

动画:

$("#box").animate({left:xxx,top:xxx,...},duration,"linear/swing",function (){});

  • stop——结束之前正在运行的动画,开启下一个新的动画
  • delay——延迟
  • finnsh
$("#box").stop().animate({...});
  • fadeIn——淡入
  • fadeOut——淡出
  • fadeToggle——
  • show
  • hide
  • toggle
  • slideUp
  • slideDown
  • slideToggle
$(function () {
	var $submit = $("#submit");
	var $box = $("#box");
	$submit.on("click", function () {
		//$box.stop().hide(500);
		//$box.stop().hide("slow");//"fast" 时间ms

		//$box.stop().slideUp(500);
		//$box.stop().fadeOut(500);

		//$box.stop().delay(1000).slideToggle(500);

		//$box.slideUp(500).stop().hide(500);
		//stop的意思是:结束上一个动画,下一个动画是紧接着当前位置开始运动的
		//$box.slideUp(500).finish().toggle(500);
		//finish的意思是:结束上一个动画,并且让元素快速的到达目标位置,再开始下一个动画
		
		//$box.stop().animate({
		//	stop:600
		//},1000,"swing")
		
		$box.stop().animate({
			stop:600,
		},1000, function () {
			$(this).css("background", "#000")
		});
	})
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions