Lớp Sư phạm Cử nhân tin học khóa 1
Chào mừng bạn trở lại diễn đàn.

JavaScript hiển thị menu ngữ cảnh (Right-Click)

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down

JavaScript hiển thị menu ngữ cảnh (Right-Click)

Bài gửi by Admin on 22/3/2008, 6:04 pm

Bạn copy đoạn mã trong thẻ <head> dán vào trang web của bạn để chạy chương trình .
Bạn cũng có thể copy toàn bộ doạn mã dưới đây và lưu thành file RightMouse.htm để chạy thử. Hoặc vào http://cnthk1.4shared.com trong muc /Thiet ke web/Giai bai tap/ de download.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - Pham Van Nam>
<html>
<head>
    <title>Bai tap Thiet ke Web voi PHP</title>
<style type="text/css">
.jsbank_sample_cont {margin: 20px; padding: 20px;}
.jsbank_sample_tit {font-weight: bold; margin-bottom: 10px; padding: 5px; width: auto; background-color: #c0c0c0; border: 5px solid #a0a0a0; color: black; text-align: center;}
</style>
<script language="javaScript">

 menuItems = new Array();
 menuItemNum = 0;

 function addMenuItem(text, url, img){
  if(img) menuItems[menuItemNum] = new Array(text, url, img);
  else if(text) menuItems[menuItemNum] = new Array(text, url);
  else menuItems[menuItemNum] = new Array();
  menuItemNum++;
 }

 menuWidth = 190; //menu width
 menuHeight = 296; //menu height
 menuDelay = 50; //delay before menu appears
 menuSpeed = 8; //speed which menu appears (lower=faster)
 menuOffset = 2; //offset of menu from mouse pointer

 addMenuItem("<center><b><u>Right-Click Menu</center>","http://localhost/menua.html");
 addMenuItem();
 addMenuItem("<b>1.Chuong trinh chat:");
 addMenuItem("&nbsp;+ DSChat","http://localhost/DSchat/index.php");
 addMenuItem("&nbsp; + ChatRoom","http://localhost/jodchat/jodchat.php");
 addMenuItem();
 addMenuItem("<b>2.Ung dung:");
 addMenuItem("&nbsp;+ Tim ten tinh thanh VietNam","http://localhost/Danhmuc/Danhmuc.html");
 addMenuItem("&nbsp;+ Danh ba dien thoai","http://localhost/danhsach/form.html");
 addMenuItem();
 addMenuItem("<b>3.Giai toan");
 addMenuItem("&nbsp;+ Cong tru 2 phan so","http://localhost/tongphanso/Tongphanso.html");
 addMenuItem("&nbsp;+ Tinh khoang cach 2 diem","http://localhost/khoangcach/khoangcach2diem.html");
 addMenuItem("&nbsp;+ Giai phuong trinh bac 2","http://localhost/phuongtrinhbac2/ptrinhb2.html");
 addMenuItem("&nbsp;+ Giai bat phuong trinh bac 1","http://localhost/batphuongtrinh/bptrinh.php");
 addMenuItem("&nbsp;+ Tinh dien tich tam giac","http://localhost/tamgiac/giaitamgiac.html");
 addMenuItem();
 addMenuItem("<b>4.Games:");
 addMenuItem("&nbsp;+Co caro","http://localhost/cocaro/cocaro.html");
 if(window.navigator.appName == "Microsoft Internet Explorer" && window.navigator.appVersion.substring(window.navigator.appVersion.indexOf("MSIE") + 5, window.navigator.appVersion.indexOf("MSIE") + 8) >= 5.5)
  isIe = 1;
 else
  isIe = 0;

 if(isIe){
  menuContent = '<table id="rightMenu" width="0" height="0" cellspacing="0" cellpadding="0" style="font:menu;color:menutext;"><tr height="1"><td style="background:threedlightshadow" colspan="4"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threedlightshadow"></td><td style="background:threedhighlight" colspan="2"></td><td style="background:threedshadow"></td><td style="background:threeddarkshadow"></td></tr><tr height="10"><td style="background:threedlightshadow"></td><td style="background:threedhighlight"></td><td style="background:threedface"><table cellspacing="0" cellpadding="0" nowrap style="font:menu;color:menutext;cursor:default;">';
  for(m=0;m<menuItems.length;m++){
  if(menuItems[m][0] && menuItems[m][2])
    menuContent += '<tr height="17" onMouseOver="this.style.background=\'highlight\';this.style.color=\'highlighttext\';" onMouseOut="this.style.background=\'threedface\';this.style.color=\'menutext\';" onClick="parent.window.location.href=\'' + menuItems[m][1] + '\'"><td style="background:threedface" width="1" nowrap></td><td width="21" nowrap><img src="' + menuItems[m][2] + '"></td><td nowrap>' + menuItems[m][0] + '</td><td width="21" nowrap></td><td style="background:threedface" width="1" nowrap></td></tr>';
  else if(menuItems[m][0])
    menuContent += '<tr height="17" onMouseOver="this.style.background=\'highlight\';this.style.color=\'highlighttext\';" onMouseOut="this.style.background=\'threedface\';this.style.color=\'menutext\';" onClick="parent.window.location.href=\'' + menuItems[m][1] + '\'"><td style="background:threedface" width="1" nowrap></td><td width="21" nowrap></td><td nowrap>' + menuItems[m][0] + '</td><td width="21" nowrap></td><td style="background:threedface" width="1" nowrap></td></tr>';
  else
    menuContent += '<tr><td colspan="5" height="4"></td></tr><tr><td colspan="5"><table cellspacing="0"><tr><td width="2" height="1"></td><td width="0" height="1" style="background:threedshadow"></td><td width="2" height="1"></td></tr><tr><td width="2" height="1"></td><td width="100%" height="1" style="background:threedhighlight"></td><td width="2" height="1"></td></tr></table></td></tr><tr><td colspan="5" height="3"></td></tr>';
  }
  menuContent += '</table></td><td style="background:threedshadow"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threedlightshadow"></td><td style="background:threedhighlight"></td><td style="background:threedface"></td><td style="background:threedshadow"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threedlightshadow"></td><td style="background:threedshadow" colspan="3"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threeddarkshadow" colspan="5"></td></tr></table>';

  menuPopup = window.createPopup();
  menuPopup.document.body.innerHTML = menuContent;
 }

 function showMenu(){
  menuXPos = event.clientX + menuOffset;
  menuYPos = event.clientY + menuOffset;

  menuXIncrement = menuWidth / menuSpeed;
  menuYIncrement = menuHeight / menuSpeed;

  menuTimer = setTimeout("openMenu(0,0)", menuDelay);
  if(iHeight < menuHeight)
  menuTimer = setTimeout("openMenu(iHeight + menuYIncrement, iWidth + menuXIncrement)", 1);
  else
  clearTimeout(menuTimer);
 }


 if(isIe) document.oncontextmenu = showMenu;
</script>

</head>

<body> <!Nhap noi dung trang web vao duoi day. >
<center><h2>Menu ngu canh</h2>  <br /><br /><br />
<h3>Bam phim phai de xem menu</h3></center><br /><br />
Luu y:<br />
 - Doan ma nay chi thuc thi tren <b>Microsoft Internet Explorer</b>  <br />
 - Ban thay duong dan trong dau ngoac kep trong cac hang: addMenuItem(<i>"ten chuong trinh","http://localhost/thu muc/ten tap tin.html"</i>)
 bang cac duong dan ma ban muon lien ket den.

</html>
Lưu ý: Nhớ thay đường dẫn để đoạn chương trình thực thi chính xác.
avatar
Admin
Admin

Tổng số bài gửi : 148
Registration date : 17/12/2007

Xem lý lịch thành viên http://cnthk1.tk

Về Đầu Trang Go down

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết