Solutions to <Learning jQuery> Chapter 3

1.  When Charles Dickensis clicked, apply the selectedstyle to it.
2.  When a chapter title (<h3 class="chapter-title">) is double-clicked, toggle the visibility of the chapter text.
3.  When the user presses the right arrow key, cycle to the next body class. The key code for the right arrow key is 39.
4.  Challenge: Use the console.log()function to log the coordinates of the mouse as it moves across any paragraph. (Note: console.log()displays its results via the Firebug extension for Firefox, Safari's Web Inspector, or the Developer Tools in Chrome).

5.  Challenge: Use .mousedown()and .mouseup()to track mouse events anywhere on the page. If the mouse button is released above where it was pressed, then add the hiddenclass to all paragraphs. If it is released below where it was pressed, then remove the hiddenclass from all paragraphs.

$(document).ready(function(){
        //foundation
	$('#switcher').hover(function(){
		$(this).addClass('hover');},function(){
		$(this).removeClass('hover');
	});
	var toggleSwitcher=function(event){
		if(!$(event.target).is('button')){
			$('#switcher button').toggleClass('hidden');
		}
	}
	$('#switcher').bind('click.collapse',toggleSwitcher);
	$('#switcher-default').addClass('selected');
	$('#switcher').bind('click',function(event){
		if($(event.target).is('button')){
			var type=event.target.id.split('-')[1];
			$('body').removeClass();
			$('body').addClass(type);
			$('#switcher button').removeClass('selected');
			$(event.target).addClass('selected');
			if($(event.target).is('#switcher-narrow,#switcher-large')){
				$('#switcher').unbind('click.collapse');
			}else if($(event.target).is('#switcher-default')){
				$('#switcher').unbind('click.collapse');
				$('#switcher').bind('click.collapse',toggleSwitcher);
			}
			event.stopPropagation();
		}
	});
	$('#switcher').trigger('click');
	
	var triggers={
		D:'default',
		N:'narrow',
		L:'large'
	};
	$(document).bind('keyup',function(event){
		var key=String.fromCharCode(event.keyCode);
		if(key in triggers){
			$('#switcher-'+triggers[key]).click();
		}
	});
	
	//solutions begin here
	//1
	$('.author').bind('click',function(){
		$(this).toggleClass('selected');
	});
	//2
	$('.chapter').dblclick(function(event){
		if($(event.target).is('.chapter-title')){
			$(this).children('p').toggleClass('hidden');
		}
	});
	//3
	var currentClass=0;
	var nextClass=1;
	var classes=['default','narrow','large'];
	$(document).keyup(function(event){
		if(event.keyCode==39){
			if($('body').hasClass('default')){
				currentClass=0;
			}else if($('body').hasClass('narrow')){
				currentClass=1;
			}else if($('body').hasClass('large')){
				currentClass=2;
			}
			nextClass=(currentClass+1)%3;
			$('body').removeClass(classes[currentClass]).addClass(classes[nextClass]);
		}
	});
	//4
	$('p').mousemove(function(event){
		console.log('coordinate:'+event.pageX+','+event.pageY+'\n');
	});
	//5
	var oldX=-1;
	var oldY=-1;
	$(document).mousedown(function(event){
		oldX=event.pageX;
		oldY=event.pageY;
	});
	$(document).mouseup(function(event){
		if(oldY==event.pageY){
			if(oldX==event.pageX){
				$('p').addClass('hidden');
			}
		}else if(oldY<event.pageY){
			$('p').removeClass('hidden');
		}
	});
});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值