Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

[ JavaScript ] Một số đoạn code JavaScript hữu ích cho Web và Blog

1. Tạo nút Back To Top (trở về đầu trang) cho Blogspot. 

Tiện ích này rất hữu ích cho các trang có nội dung dài.




- Bước 1: vào mục Mẫu >> Chỉnh sửa HTML
Thêm đoạn code sau vào trước thẻ </body> (Để vào sau <head> cũng được nhưng nên để code cuối trang vì ta sẽ để load sau cùng):

<style type='text/css'>
#bttop{ 
border:1px solid 
#4adcff;background: 
#24bde2;text-align:center;padding:5px;position:fixed;bottom:35px;right:10px;cursor:pointer;display:none;color: 
#fff;font-size:11px;font-weight:900;line-height: 25px; 
#bttop:hover{border:1px solid #32cd32;background:#bada55; 
}
</style>
<div id='bttop'>BACK TO TOP</div>
<script src='https://backlinkjs.googlecode.com/files/min.js' type='text/javascript'></script>
<script type='text/javascript'>$(function(){$(window).scroll(function(){ 
    if($(this).scrollTop()!=0){ 
       $('#bttop').fadeIn(); 
    }else{$('#bttop').fadeOut(); 
   } 
  }); 
  $('#bttop').click(function(){
$('body,html').animate({scrollTop:0},800); 
}); 
}); 
</script>

Chú ý: Ở giữa 2 thẻ <style type='text/css'> và </style> là đoạn css định hình dạng cho nút Back to Top, các bạn tùy chỉnh thoải mái. Dòng code màu lam là thư viện Jquery, nếu có rồi thì các bạn nên xóa nó đi. BACK TO TOP các bạn thay bằng gì cũng được (GO TOP chẳng hạn).
Bạn có thể download file min.js [tại đây - click bỏ qua quảng cáo để download] sau đó bạn có thể upload lên google code để lưu trữ lâu dài.

2. Không cho click phải chuột
<SCRIPT language=JavaScript1.1>
function right(e) {
if (navigator.appName == 'Netscape' &&
(e.which == 3 || e.which == 2))
return false;
else if (navigator.appName == 'Microsoft Internet Explorer' &&
(event.button == 2 || event.button == 3)) {
alert('Chuc Mot Ngay Vui ve - Gui Wedsite Cho Ban Be Cua Minh Nha !');
return false;
}
return true;
}
document.onmousedown=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
window.onmousedown=right;
</SCRIPT>
3.   Làm Thanh Tiêu Đề Web Chạy Trên Task Manager Khi Đóng Mở Web
<script language=javascript >
title_tmp1=document.title
if (title_tmp1.indexOf(">>")!=-1){
title_tmp2=title_tmp1.split(">>");
title_last="*~*"+title_tmp2[1];
title_last=title_last + "*~*" + title_tmp2[2];
}else{
if (title_tmp1.indexOf("*~*")!=-1){
title_tmp2=title_tmp1.split("*~*");
title_last="*~*"+title_tmp2[1];
if (title_last=="*~*"){title_last="*~*"};
if (title_last=="*~*"){title_last="*~*"};
}
else { title_last=" wWw.KyNiemB2.Top1.Vn "}
}

title_new=""+title_last+""
step=0
function flash_title()
{
step++
if (step==8) {step=1}
if (step==1) {document.title='[----*'+title_new+'*----]'}
if (step==2) {document.title='[---*-'+title_new+'-*---]'}
if (step==3) {document.title='[--*--'+title_new+'--*--]'}
if (step==4) {document.title='[-*---'+title_new+'---*-]'}
if (step==5) {document.title='[--*--'+title_new+'--*--]'}
if (step==6) {document.title='[---*-'+title_new+'-*---]'}
if (step==7) {document.title='[----*'+title_new+'*----]'}
setTimeout("flash_title()",180);
}
flash_title()
</script>
4.  Chống Click Chuột Phải ... Và Làm "Giật Giật Web Nêu Người Xem Cứ Cố ý Click Right ...

<SCRIPT language=JavaScript>
document.onmousedown=click
var times=0
var times2=10
function click() {
if ((event.button==2) || (event.button==3)) {
if (times>=1) { earthquake() }
alert("Ban Ko Dc Click Chuot Phai");
times++ } }
function earthquake() {
alert("Ban Ko Dc Click Chuot Phai");
window.moveTo(0, 0)
window.moveTo(1, 1)
window.moveTo(2, 2)
window.moveTo(3, 3)
window.moveTo(4, 4)
window.moveTo(5, 5)
window.moveTo(6, 6)
window.moveTo(7, 7)
window.moveTo(8, 8)
window.moveTo(9, 9)
window.moveTo(10, 10)
window.moveTo(9, 9)
window.moveTo(8, 8)
window.moveTo(7, 7)
window.moveTo(6, 6)
window.moveTo(5, 5)
window.moveTo(4, 4)
window.moveTo(3, 3)
window.moveTo(2, 2)
window.moveTo(1, 1)
alert("Giat Nhu Fiml Nha??!")
tremmors()
}
function tremmors() {
window.moveTo(0, 0)
window.moveTo(1, 1)
window.moveTo(2, 2)
window.moveTo(3, 3)
window.moveTo(4, 4)
window.moveTo(5, 5)
window.moveTo(6, 6)
window.moveTo(7, 7)
window.moveTo(8, 8)
window.moveTo(9, 9)
window.moveTo(10, 10)
window.moveTo(11, 11)
window.moveTo(12, 12)
window.moveTo(9, 9)
window.moveTo(8, 8)
window.moveTo(7, 7)
window.moveTo(6, 6)
window.moveTo(5, 5)
window.moveTo(4, 4)
window.moveTo(3, 3)
window.moveTo(2, 2)
window.moveTo(1, 1)
tremmors()
}
</SCRIPT>
5.  Làm Con Chuột Có Mấy Cái Vòng Chạy Chạy Xung Quanh ...

<script language="JavaScript">
<!-- Mouse Attack by Kurt Grigg - http://www.btinternet.com/~kurt.grigg/javascript
var num=10;//Number of dots!
var vel=20;//Speed!
var col=new Array('#0080ff','#ffffff','#0000ff','#44ccff');//Dot colours, min 2!
var stopafter=60; //Stop and clear after x secondS!
//Nothing needs altering past here....................
var MAy=0;
var MAx=0;
var py=0;
var px=0;
var angle=0;
var distance=0;
var rep;
var ry=0;
var rx=0;
var tmr=null;
var n4=(document.layers);
var n6=(document.getElementById&&!document.all);
var ie=(document.all);
var o6=(navigator.appName.indexOf("Opera") != -1)?true:false;
var _d=(n4||ie)?'document.':'document.getElementById(" ';
var _a=(n4||n6)?'':'all.';
var _r=(n6)?'")':'';
var _s=(n4)?'':'.style';
var v=(n4)?"show":"visible";
var put=false;
stopafter*=1000;
if (n4||n6){
window.captureEvents(Event.MOUSEMOVE);
function mouse1(e){
if (put) return false;
MAy = e.pageY-window.pageYOffset;
MAx = e.pageX;
}
if (n4) window.onMouseMove=mouse1;
else document.onmousemove=mouse1;
}
if (ie||o6){
function mouse2(){
if (put) return false;
MAy = (ie)?event.clientY:event.clientY-window.pageYOffset;
MAx = event.clientX;
}
document.onmousemove=mouse2;
}
if (n4){
for (inc=0; inc < num; inc++)
document.write('<layer name=dots'+inc+' top=0 left=0 width='+inc/3+' height='+inc/3+' bgcolor=#ffffff></layer>');
}
else{
if (ie&&!o6){
document.write("<div id='outer' style='position:absolute;top:0px;left:0px'>");
document.write("<div style='position:relative'>");
}
for (inc=0; inc < num; inc++)
document.write('<div id="dots'+inc+'" style="position:absolute;top:0px;left:0px;width:'+ inc/3+';height:'+inc/3+';background:#ffffff;font-size:1">.</div>');
if (ie&&!o6) document.write("</div></div>");
}
function pos(){
h=(ie)?document.body.clientHeight:window.innerHeig ht-20;
w=(ie)?document.body.clientWidth:window.innerWidth-20;
ry=Math.round(Math.random()*h);
rx=Math.round(Math.random()*w);
rep=Math.round(Math.random()*3);
if (rep == 3)ry=0;
if (rep == 2)ry=h;
if (rep == 1)rx=0;
if (rep == 0)rx=w;
py=ry;
px=rx;
}
pos();
function followleader(){
sy=(!ie)?window.pageYOffset:0;
sx=(!ie)?window.pageXOffset:0;
if (ie) outer.style.top=document.body.scrollTop;
for (inc=0; inc < num; inc++){
randomcol=col[Math.floor(Math.random()*col.length)];
temp1=eval(_d+_a+"dots"+inc+_r+_s);
temp1.visibility=v;
if (inc < num-1){
temp2=eval(_d+_a+"dots"+(inc+1)+_r+_s);
temp1.top=temp2.top;
temp1.left=temp2.left
}
else{
temp1.top=py+sy;
temp1.left=px;
}
if (n4)temp1.bgColor=randomcol;
else temp1.background=randomcol;
}
}
function bomb(){
ay=MAy-py;
ax=MAx-px;
angle=Math.round(Math.atan2(ay,ax)*180/Math.PI);
if (angle < 0) angle += 360;
MAdy=py-MAy;
MAdx=px-MAx;
distance=Math.floor(Math.sqrt(MAdx*MAdx+MAdy*MAdy) );
go_y = Math.round(vel*Math.sin(angle*Math.PI/180));
go_x = Math.round(vel*Math.cos(angle*Math.PI/180));
py+=go_y;
px+=go_x;
if (distance < vel) pos();
followleader();
tmr=setTimeout("bomb()",20);
}
bomb();
function dsbl(){
v=(n4)?"hide":"hidden";
put=true;
MAx=0;
MAy=0;
setTimeout('clearTimeout(tmr)',stopafter+100);
}
setTimeout('dsbl()',stopafter);
//-->
</script>
6. TITLE Chạy .....

 <script language=JavaScript>
var txt=" ..:: WelCome To Website | wWw.KyNiemB2.Top1.Vn | Mang Giai Tri Online -> 12B2 THPT EaH'Leo - Daklak - Design : HoangVu Entertainment© -";
var expert=200;
// speed of roll
var refresh=null;
function marquee_title(){
document.title=txt;
txt=txt.substring(1,txt.lenghth)+txt.charAt(0);
refresh=setTimeout("marquee_title()",expert);
}
marquee_title();
</script>
7. Dòng Chữ Chạy Dưới web ...

<SCRIPT language=JavaScript>
puchtit=")«-»(..:: Welcome To WwW.NhoOi.Kiss.To ::..)«-»(";
letrero2="·.¸¸.·´´¯`··._.·";
letrero1="·.¸¸.·´´¯`··._.·";ultimo1=letr ero1.length-1;
ultimo2=letrero2.length-1;
tiempo=setTimeout("scroll()",.100);
function scroll()
{
aux1=letrero1.charAt(ultimo1-1);
letrero1=aux1+letrero1.substring(0,ultimo1-1);
aux2=letrero2.charAt(0);
letrero2=letrero2.substring(1,ultimo2+1)+aux2;
window.status="(" + letrero1 + ")(" + letrero2 + puchtit + letrero1 + ")(" + letrero2 + ")";
tiempo=setTimeout("scroll()",.100);
return true;
}
// -->
</script>
8. Tạo 1 Button Giờ Trên Website
 <SCRIPT language=JavaScript>
<!-- Begin
day = new Date();
miVisit = day.getTime();
function clock() {
dayTwo = new Date();
hrNow = dayTwo.getHours();
mnNow = dayTwo.getMinutes();
scNow = dayTwo.getSeconds();
miNow = dayTwo.getTime();
if (hrNow == 0) {
hour = 12;
ap = " AM";
} else if(hrNow <= 11) {
ap = " AM";
hour = hrNow;
} else if(hrNow == 12) {
ap = " PM";
hour = 12;
} else if (hrNow >= 13) {
hour = (hrNow - 12);
ap = " PM";
}
if (hrNow >= 13) {
hour = hrNow - 12;
}
if (mnNow <= 9) {
min = "0" + mnNow;
}
else (min = mnNow)
if (scNow <= 9) {
secs = "0" + scNow;
} else {
secs = scNow;
}
time = hour + ":" + min + ":" + secs + ap;
document.form.button.value = time;
self.status = time;
setTimeout('clock()', 1000);
}
function timeInfo() {
milliSince = miNow;
milliNow = miNow - miVisit;
secsVisit = Math.round(milliNow / 1000);
minsVisit = Math.round((milliNow / 1000) / 60);
alert("There have been " + milliSince + " milliseconds since midnight, January 1, 1970. "
+ "You have spent " + milliNow + " of those milliseconds on this page. "
+ ".... About " + minsVisit + " minutes, and "
+ secsVisit + " seconds.");
}
document.write("<center><form name=\"form\">"
+ "<input type=button value=\"Click for info!\""
+ " name=button onClick=\"timeInfo()\"></form></center>");
onError = null;
clock();
// End -->
</SCRIPT> 
9. Bộ Đếm 10 , 9 ....0
 <!---------------------------------------- NOJ DUNG --------------------->
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<!------------------------------- Thanh Trượt ---->
<STYLE>BODY A:link {
COLOR: #253d59; TEXT-DECORATION: none
}
A:visited {
COLOR: #253d59; TEXT-DECORATION: none
}
A:hover {
padding:0px; BACKGROUND: #ffffff; MARGIN: 0px; COLOR: #253d59; tahoma:
}
A:active {
padding:0px; BACKGROUND: #ffffff; MARGIN: 0px; COLOR: #253d59; tahoma:
}
HTML {
SCROLLBAR-FACE-COLOR: #ffffff; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #ffffff; SCROLLBAR-3DLIGHT-COLOR: #ffffff; SCROLLBAR-ARROW-COLOR: #8b8b8b; SCROLLBAR-TRACK-COLOR: #ffffff; SCROLLBAR-DARKSHADOW-COLOR: #7a7a7a
}
BODY {
SCROLLBAR-FACE-COLOR: #ffffff; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #ffffff; SCROLLBAR-3DLIGHT-COLOR: #ffffff; SCROLLBAR-ARROW-COLOR: #8b8b8b; SCROLLBAR-TRACK-COLOR: #ffffff; SCROLLBAR-DARKSHADOW-COLOR: #7a7a7a
}
</STYLE>
<!------------------------- Kieu Chu ----------------->
<META content="Microsoft FrontPage 5.0" name=GENERATOR></HEAD>
<BODY>
<CENTER><B><FONT style="FONT-SIZE: 40pt" face="Comic Sans MS"
color=#ffff00>[</FONT><FONT style="FONT-SIZE: 40pt" face="Comic Sans MS"
color=#ff3333> <A href="http://www.kyniemb2.top1.vn/">w</A></FONT><A
href="http://www.kyniemb2.top1.vn/"><FONT style="FONT-SIZE: 40pt"
face="Comic Sans MS" color=#cc00ff>W</FONT><FONT style="FONT-SIZE: 40pt"
face="Comic Sans MS" color=#ff3333>w.</FONT><FONT style="FONT-SIZE: 40pt"
face="Comic Sans MS">K<FONT color=#00ff00>y</FONT><FONT
color=#ff0000>Ni</FONT>e<FONT color=#ffff00>m</FONT><FONT
color=#cc00ff>B2</FONT>.T<FONT color=#00ff00>o</FONT>p<FONT
color=#ff3399>1</FONT>.V<FONT color=#ff0000>n</FONT></FONT></A><FONT
style="FONT-SIZE: 40pt" face="Comic Sans MS" color=#ff3333> </FONT><FONT
style="FONT-SIZE: 40pt" face="Comic Sans MS" color=#ffff00>]<BR></FONT><FONT
face="Comic Sans MS" color=#ff3399 size=6>Thay Đôi? Tên Miê`n Chính Thú*c
</FONT></B>
<DIV id=txt align=center></DIV><!--------------------- Doạn CODE ---------------->
<SCRIPT type=text/javascript>
var c=10;
var t;
var theDate = new Date();
s=theDate.getSeconds();
function ChangeUrl(site){
document.getElementById("txt").innerHTML='<br><br> <br><h3><a href="http://www.NHOOI.KISS.TO" target=Index><font face="Comic Sans MS" color=3366CC></a><br><a href="'+site+'" target=Index><font color=FF00FF><big>Click vào đây đê? vào Trang Chu? </big></font></font></a></h3><br><br><br><p align="center"><FONT face="Tahoma">Trình duyệt sẽ tự động chuyển sang <b><font color=blue>'+site+'</font></b> trong vòng <b><font color=red>'+c+'</font></b> gi&acirc;y nữa.<br></Font>';
document.getElementById("txt").innerHTML+='<strong ><a href="'+site+'" target="_top" onClick="this.style.behavior=\'url(#default#homepa ge)\';this.setHomePage(\'http://nhooi.kiss.to\');"><Font Face="Tahoma">Click vào đ&acirc;y nếu bạn chờ qu&aacute; l&acirc;u [ Click ] Vao Day Cung Duoc !^^.</a></strong><br></span></p></Font>';
c-=1;
url=site;
t=setTimeout("ChangeUrl(url)",1000);
stopCount(c,url);
}
function stopCount(c,link){
if(c==-1)
{clearTimeout(t);
location.href=link;}
}
</SCRIPT>
<SCRIPT>
ChangeUrl('Http://KyNiemB2.Top1.Vn');
</SCRIPT>
<!---------------- Chu 2 -------------------->
<FONT face="Comic Sans MS" size=6><B><FONT
color=#ff0000>Design</FONT></B><B><FONT color=#00ff00> </FONT><FONT
color=#cc00ff>:</FONT><FONT color=#00ff00> </FONT><FONT
color=#ffff00>Hô`</FONT><FONT color=#00ff00> Ho</FONT>àng<FONT color=#00ff00>
</FONT><FONT color=#0000ff>Vũ</FONT><FONT color=#00ff00> </FONT><FONT
color=#cc00ff>®</FONT><FONT color=#00ff00><BR></FONT></B><FONT
color=#ffff00>Website</FONT><FONT color=#00ff00>
:wWw.KyNiemB2.Top1.Vn</FONT></FONT><FONT color=#00ff00
size=6><BR></FONT></CENTER>
</TEXTAREA></P></BODY></HTML> 

[Tải Code Chương trình tại đây (Lưu ý: Sau 5s, click Bỏ qua quảng cáo - Skin Ad)]

Giới thiệu CSS


CSS là gì?

CSS là chữ viết tắt của Cascading Style Sheets, nó chỉ đơn thuần là một dạng file text với phần tên mở rộng là .css. Trong Style Sheet này chứa những câu lệnh CSS. Mỗi một lệnh của CSS sẽ định dạng một phần nhất định của HTML ví dụ như: font của chữ, đường viền, màu nền, căn chỉnh hình ảnh v.v..
Trước đây khi chưa có CSS, những người thiết kế web phải trộn lẫn giữa các thành phần trình bày và nội dung với nhau. Nhưng với sự xuất hiện của CSS, người ta có thể tách rời hoàn toàn phần trình bày và nội dung. Giúp cho phần code của trang web cũng gọn hơn và quan trọng hơn cả là dễ chỉnh sửa hơn.
Trước khi bắt đầu tìm hiểu thêm về CSS tôi muốn bạn biết một chút về thế mạnh của nó trong việc thiết kế web. Ví dụ nếu làm việc với HTML và bạn muốn font chữ của toàn bộ trang web là Arial. Bạn sẽ phải làm đi làm lại như thế cho tất cả các file .html mà bạn có. Nhưng nếu bạn sử dụng CSS, thì bạn chỉ cần làm một lần và tất cà các trang khác sẽ tự động được thay đổi.
Ba cách để định dạng trang web

Có ba cách bạn có thể sử dụng để định dạng trang web là: cục bộ, nhúng vào trang và liên kết đến một file CSS riêng biệt. Trong thực tế thì cách cuối cùng là liên kết đến một file riêng biệt được sử dụng phổ biến nhất. Nhưng bạn cũng nên biết về hai cách còn lại vì đôi khi bạn cũng phải sử dụng đến nó tuy không nhiều
.
1. Cục bộ – code CSS được chèn trực tiếp vào trong thẻ HTML

Cách định dạng cục bộ là bạn sử dụng code CSS chèn trực tiếp vào thẻ HTML và chỉ có tác động lên thẻ đó.

Ví dụ

<p style=”font-size:16pt; font-weight:bold; color:blue;”> 
Nếu bạn thêm code CSS vào trong thẻ này nó sẽ thay đổi cách hiển thị trên trình duyệt
</p>

2. Nhúng vào trang web

Cách thứ hai là bạn khai báo code CSS nằm trong cặp thẻ <style></style> được đặt ở phần <head></head> của tài liệu. Về cơ bản nó cũng chỉ có tác động cục bộ lên file mà bạn chèn đoạn code CSS này. Nó có phạm vi ảnh hưởng lớn hơn là cách chèn cục bộ được nói ở trên, nhưng nó không có ảnh hưởng đến các file khác trong cùng một trang web.

<head><title>Nhúng vào trang</title><meta http-equiv=”Content-type” content=”text/html; charset=utf-8″ /><style type=”text/css”>h1 {font-size: 16px;}p {color:blue;}</style></head>

3. Liên kết đến một file biệt lập

Thực tế cách cuối cùng này mới là thế mạnh thực sự của CSS bởi vì bạn chỉ cần tạo ra một file CSS và viết code chỉ một lần. Nó sẽ ảnh hưởng đến toàn bộ trang web chứ không chỉ ảnh hưởng đến một file .html đơn lẻ hoặc chỉ một thẻ trong hàng ngàn thẻ mà bạn có. Bằng cách này chúng ta có thể tách rời hoàn toàn những thành phần trình bày và nội dung ra khỏi nhau. Nếu bạn viết toàn bộ code CSS trong một file và liên kết nó đến những trang còn lại, thì việc thay đổi sửa chữa trang web trở nên cực kỳ đơn giản.
Để liên kết file CSS đến tất cả các file .html của bạn, bạn chỉ cần chèn đoạn code sau vào giữa thẻ <head></head> của tài liệu:

<link href="style_sheet.css" rel="stylesheet" type="text/css" />

Và trình duyệt sẽ tự động dùng file style_sheet.css để định dạng cho trang web của bạn. Tất nhiên trong file style_sheet.css bạn đã viết code CSS rồi.
Điều cuối cùng bạn cần biết về 3 loại hình sử dụng CSS là
Cục bộ > Nhúng vào trang > Liên kết đến một file biệt lập
Công thức trên có nghĩa là cách Cục bộ “đánh bại” cách Nhúng vào trang và Liên kết đến một file riêng biệt. Hoặc nói theo cách khác thì cách nhúng cục bộ là mạnh nhất, sau đó đến Nhúng vào trang và cuối cùng là liên kết đến một file biệt lập.

Tài liệu có tại http://kcc.net.vn