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).
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');
}
});
});
本文介绍了一种使用JavaScript实现的网页交互功能,包括点击作者名切换样式、双击章节标题显示或隐藏章节内容、通过键盘右箭头键改变页面样式等。此外,还介绍了如何利用鼠标事件和控制台日志功能提升用户体验。
2533

被折叠的 条评论
为什么被折叠?



