Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > การสร้าง Checkbox และ Radio Button ให้เป็นรูปภาพ (หรืออื่นๆ) ด้วย CSS



 
Clound SSD Virtual Server

การสร้าง Checkbox และ Radio Button ให้เป็นรูปภาพ (หรืออื่นๆ) ด้วย CSS

การสร้าง Checkbox และ Radio Button ให้เป็นรูปภาพ (หรืออื่นๆ) ด้วย CSS

จากกระทู้ https://www.thaicreate.com/php/forum/113253.html จึงอยากแนะนำวิธีการใช้ CSS ในการทำครับ
แต่เทคนิคนี้ใช้ได้ตั้งแต่ IE9 ขึ้นไปนะครับสำหรับ IE

Styling checkbox with CSS


ซึ่งหัวใจหลักของเทคนิคนี้คือ tag <label> และ :checked CSS selector

ซึ่ง tag <label> โดยปกติถ้าครอบ checkbox และ radio และผสมด้วย text แม้จะไม่ได้คลิกตรง checkbox/radio โดยตรง ก็จะเสมือนคลิกโดน

<label>
    <input type="checkbox" name="test" value="Hello World">
    Test
</label>


ถ้าคลิกคำว่า Test checkbox ก็จะทำงานได้ปกติ คือสลับระหว่างสถานะ checked/unchecked

จากการทำงานของ <label> เราสามารถประยุกต์ใช้สร้าง checker (checkbox/radio) ที่เป็น <img> หรืออื่นๆ ได้
โดยเราจะใช้ CSS adjacent selector (+) ในการทำให้ element ที่เราต้องการแสดงผลในสถานะที่ต่างกัน
ซึ่ง + หมายความว่า ให้เลือก element ที่อยู่หลัง element นี้

ตัวอย่าง
h1 + p {
    color: red;
}
/*
tag <p> ที่อยู่หลัง tag <h1> จะมีสีข้อความเป็นสีแดง
เช่น
<h1>Hello World</h1>
<p>This will be red!</p>
*/


ดังนั้นถ้าเราซ่อน checkbox ไว้ด้วย display: none และใช้ + เพื่อจัดรูปแบบ element ถัดไปจาก checkbox

<style>
input[type="checkbox"] {
    display: none;
}
input[type="checkbox"] + span {
    cursor: pointer;
}
// span ที่อยู่หลัง checkbox จะมีสีแดงเมื่อ checkbox มีสถานะเป็น checked
input[type="checkbox"]:checked + span {
    color: red;
}
</style>
...
<label>
    <input type="checkbox" name="test" value="Hello World">
    <span>Test</span>
    <!--
    <span> อันนี้จะเป็นจุดที่สามารถคลิกได้แทน checkbox เพราะถูกครอบด้วย <label>
    -->
</label>


ดังนั้นเราจึงสามารถแสดงผล checker ให้เป็นอะไรก็ได้ ไม่ว่าจะเป็นข้อความหรือรูปภาพ โดยที่ checker ยังทำงานตามปกติ สามารถส่งค่าผ่าน <form> ได้เหมือนเดิม

ตัวอย่าง
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<style>
			body {
				font-family: sans-serif;
			}
			/*
			ให้ element ที่มี class img-checker ถูกซ่อนไว้
			*/
			.img-checker {
				display: none;
			}
			/*
			ให้ <img> ที่ตามหลัง element ที่มี class img-checker แสดงผลแบบจาง
			และใช้ cursor รูปมือเมื่อผู้ใช้เอาเมาส์ไปชี้
			*/
			.img-checker + img {
				opacity: 0.5;
				cursor: pointer;
			}
			/*
			ให้ <img> ที่ตามหลัง element ที่มี class img-checker กลับมาแสดงผลแบบปกติ
			เมื่อ element ที่มี class img-checker มีสถานะเป็น checked
			*/
			.img-checker:checked + img {
				opacity: 1.0;
			}
		</style>
	</head>
	<body>
		<h3>Checkboxes</h3>
		<form>
			<label>
				<input type="checkbox" name="test[]" value="All" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/all.png?v=1001">
			</label>
			<label>
				<input type="checkbox" name="test[]" value="PHP" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/php.png?v=1001">
			</label>
			<label>
				<input type="checkbox" name="test[]" value="ASP" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/asp.png?v=1001">
			</label>
			<label>
				<input type="checkbox" name="test[]" value=".NET" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/dotnet.png?v=1001">
			</label>
			<label>
				<input type="checkbox" name="test[]" value="Java" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/java.png?v=1001">
			</label>
			<label>
				<input type="checkbox" name="test[]" value="Mobile" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/mobile.png?v=1001">
			</label>
			<br>
			<br>
			<input type="button" onclick="alert(Array.prototype.slice.call(this.form.elements['test[]']).map(function (i) { return i.checked ? i.value : false; }).filter(Boolean));" value="Check Values">
		</form>
		<hr>
		<h3>Radios</h3>
		<form>
			<label>
				<input type="radio" name="test" value="All" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/all.png?v=1001">
			</label>
			<label>
				<input type="radio" name="test" value="PHP" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/php.png?v=1001">
			</label>
			<label>
				<input type="radio" name="test" value="ASP" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/asp.png?v=1001">
			</label>
			<label>
				<input type="radio" name="test" value=".NET" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/dotnet.png?v=1001">
			</label>
			<label>
				<input type="radio" name="test" value="Java" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/java.png?v=1001">
			</label>
			<label>
				<input type="radio" name="test" value="Mobile" class="img-checker">
				<img src="https://www.thaicreate.com/images/resource/mobile.png?v=1001">
			</label>
			<br>
			<br>
			<input type="button" onclick="alert(this.form.elements.test.value);" value="Check Value">
		</form>
	</body>
</html>


ผลลัพธ์
Styling checkbox with CSS








   
Share
Bookmark.   

  By : phpinfo()
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2014-12-16
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่