วิธีทำปุ่ม Share ลง LINE และ Social ในเว็บไซต์ ด้วย HTML CSS JavaScript

สำหรับวันนี้เราจะมาเรียนรู้เรื่อง การทำปุ่ม Share ลง Social นะครับ โดยในบทความนี้ จะสอนการทำปุ่ม Share ลง Social ทุกแพลตฟอร์ม รวมไปถึง Line ด้วยครับ ถ้าพร้อมแล้วไปลุยกันเลย

CSS Code

สำหรับตกแต่ง Icon Share ของเรา

							
							
					<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
integrity="sha512-..." crossorigin="anonymous" referrerpolicy="no-referrer" />

<style>
/* เปลี่ยนชื่อ Class โดยเติม custom-share- เพื่อป้องกันการชนกับ CSS ส่วนอื่น */
.custom-share-wrapper {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    gap: 0px; /*ระยะห่าง icon*/
    justify-content: flex-end; /*จัดซ้าย ขวา กลาง*/
}

.custom-share-wrapper li {
    display: inline-block;
}

.custom-share-wrapper a {
    text-decoration: none;
}

/*ขนาด icon*/
.custom-share-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 16px;
    transition: all 0.25s ease;
}

/* สีแต่ละปุ่ม */
.custom-share-bg-facebook { background: #1877F2; }
.custom-share-bg-x { background: #000; }
.custom-share-bg-line { background: #00C300; }
.custom-share-bg-copy { background: #E74C6A; }

/* Hover effect */
.custom-share-icon:hover {
    transform: translateY(-5px) scale(1.05);
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
</style>				
			

หากต้องการจัดกลุ่มของไอคอนซ้าย กลาง ขวา

ให้แทนที่ Code ชุดด้านล่างในบรรทัดภายใน .custom-share-wrapper ในส่วนของคำสั่ง justify-content ให้ใส่ตามความต้องการการจัดวางของเรา ดังนี้

  1. flex-start จัดกลุ่มของ icon ชิดซ้าย
  2. flex-end จัดกลุ่มของ icon ชิดขวา
  3. center จัดกลุ่มของ icon ชิดกลาง
							
							
					
.custom-share-wrapper {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    gap: 0px; /*ระยะห่าง icon*/
    justify-content: flex-end; /*จัดซ้าย ขวา กลาง*/
}
				
			

ในเคสตัวอย่างนี้ ผมใส่ justify-content:flex-end คือให้กลุ่มของไอคอนชิดขวา

HTML Code หลักสำหรับปุ่ม

โค้ดหลัก HTML สำหรับแสดง Icon Share ของเรา สามารถ copy ไปใช้ได้เลย

							
							
					
<ul class="custom-share-wrapper">
    <li>
        <a class="custom-share-btn-facebook" href="#" title="Share on Facebook">
            <span class="custom-share-icon custom-share-bg-facebook"><i class="fa-brands fa-facebook-f"></i></span>
        </a>
    </li>

    <li>
        <a class="custom-share-btn-x" href="#" title="Share on X">
            <span class="custom-share-icon custom-share-bg-x"><i class="fa-brands fa-x"></i></span>
        </a>
    </li>

    <li>
        <a class="custom-share-btn-line" href="#" title="Share on LINE">
            <span class="custom-share-icon custom-share-bg-line"><i class="fa-brands fa-line"></i></span>
        </a>
    </li>

    <li>
        <a class="custom-share-btn-copy" href="#" title="Copy Link">
            <span class="custom-share-icon custom-share-bg-copy"><i class="fa-solid fa-link"></i></span>
        </a>
    </li>
</ul>
				
			

JS Code

โค้ดสำหรับควบคุมฟังก์ชันการทำงานของการ copy เวลาผู้ใช้คลิกปุ่ม share icon จะ share ลงแพลตฟอร์มต่างๆตามชนิดของปุ่ม

							
							
					<script>
document.addEventListener("DOMContentLoaded", function() {
    const currentURL = encodeURIComponent(window.location.href);

    // Facebook
    document.querySelector(".custom-share-btn-facebook").href =
        "https://www.facebook.com/sharer/sharer.php?u=" + currentURL;

    // X (Twitter)
    document.querySelector(".custom-share-btn-x").href =
        "https://x.com/intent/tweet?url=" + currentURL;

    // LINE
    document.querySelector(".custom-share-btn-line").href =
        "https://line.me/R/msg/text/?" + currentURL;

    // Copy Link
    document.querySelector(".custom-share-btn-copy").addEventListener("click", function(e){
        e.preventDefault();
        navigator.clipboard.writeText(window.location.href);
        alert("คัดลอกลิงก์แล้ว!");
    });
});
</script>				
			

รวมโค้ดสำเร็จ

รวมโค้ดทุกอย่าง สามารถ copy ไปใช้งานได้เลย

							
							
					<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
integrity="sha512-..." crossorigin="anonymous" referrerpolicy="no-referrer" />

<style>
/* เปลี่ยนชื่อ Class โดยเติม custom-share- เพื่อป้องกันการชนกับ CSS ส่วนอื่น */
.custom-share-wrapper {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    gap: 0px; /*ระยะห่าง icon*/
    justify-content: flex-end; /*จัดซ้าย ขวา กลาง*/
}

.custom-share-wrapper li {
    display: inline-block;
}

.custom-share-wrapper a {
    text-decoration: none;
}

/*ขนาด icon*/
.custom-share-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 16px;
    transition: all 0.25s ease;
}

/* สีแต่ละปุ่ม */
.custom-share-bg-facebook { background: #1877F2; }
.custom-share-bg-x { background: #000; }
.custom-share-bg-line { background: #00C300; }
.custom-share-bg-copy { background: #E74C6A; }

/* Hover effect */
.custom-share-icon:hover {
    transform: translateY(-5px) scale(1.05);
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
</style>

<ul class="custom-share-wrapper">
    <li>
        <a class="custom-share-btn-facebook" href="#" title="Share on Facebook">
            <span class="custom-share-icon custom-share-bg-facebook"><i class="fa-brands fa-facebook-f"></i></span>
        </a>
    </li>

    <li>
        <a class="custom-share-btn-x" href="#" title="Share on X">
            <span class="custom-share-icon custom-share-bg-x"><i class="fa-brands fa-x-twitter"></i></span>
        </a>
    </li>

    <li>
        <a class="custom-share-btn-line" href="#" title="Share on LINE">
            <span class="custom-share-icon custom-share-bg-line"><i class="fa-brands fa-line"></i></span>
        </a>
    </li>

    <li>
        <a class="custom-share-btn-copy" href="#" title="Copy Link">
            <span class="custom-share-icon custom-share-bg-copy"><i class="fa-solid fa-link"></i></span>
        </a>
    </li>
</ul>

<script>
document.addEventListener("DOMContentLoaded", function() {
    const currentURL = encodeURIComponent(window.location.href);

    // Facebook
    document.querySelector(".custom-share-btn-facebook").href =
        "https://www.facebook.com/sharer/sharer.php?u=" + currentURL;

    // X (Twitter)
    document.querySelector(".custom-share-btn-x").href =
        "https://x.com/intent/tweet?url=" + currentURL;

    // LINE
    document.querySelector(".custom-share-btn-line").href =
        "https://line.me/R/msg/text/?" + currentURL;

    // Copy Link
    document.querySelector(".custom-share-btn-copy").addEventListener("click", function(e){
        e.preventDefault();
        navigator.clipboard.writeText(window.location.href);
        alert("คัดลอกลิงก์แล้ว!");
    });
});
</script>				
			

ตัวอย่างปุ่มแชร์ที่ได้

และนี่คือตัวอย่างปุ่มแชร์ที่ได้

ตัวอย่างผลลัพธ์

และนี่คือตัวอย่างผลลัพธ์การใช้งาน

เราใช้คุกกี้เพื่อพัฒนาประสิทธิภาพ และประสบการณ์ที่ดีในการใช้เว็บไซต์ของคุณ คุณสามารถศึกษารายละเอียดได้ที่ นโยบายความเป็นส่วนตัว และสามารถจัดการความเป็นส่วนตัวเองได้ของคุณได้เองโดยคลิกที่ ตั้งค่า

Privacy Preferences

คุณสามารถเลือกการตั้งค่าคุกกี้โดยเปิด/ปิด คุกกี้ในแต่ละประเภทได้ตามความต้องการ ยกเว้น คุกกี้ที่จำเป็น

ยอมรับทั้งหมด
Manage Consent Preferences
  • คุกกี้ที่จำเป็น
    Always Active

    ประเภทของคุกกี้มีความจำเป็นสำหรับการทำงานของเว็บไซต์ เพื่อให้คุณสามารถใช้ได้อย่างเป็นปกติ และเข้าชมเว็บไซต์ คุณไม่สามารถปิดการทำงานของคุกกี้นี้ในระบบเว็บไซต์ของเราได้

  • คุกกี้สำหรับการติดตามทางการตลาด

    ประเภทของคุกกี้ที่มีความจำเป็นในการใช้งานเพื่อการวิเคราะห์ และ นำเสนอโปรโมชัน สินค้า รวมถึงหลักสูตรฟรี และ สิทธิพิเศษต่าง ๆ คุณสามารถเลือกปิดคุกกี้ประเภทนี้ได้โดยไม่ส่งผลต่อการทำงานหลัก เว้นแต่การนำเสนอโปรโมชันที่อาจไม่ตรงกับความต้องการ

บันทึก