Read more link (jQuery)

Read more link (jQuery)

Mungkin teman-teman bertanya-tanya, bagaimana caranya membuat tombol read-more yang berfungsi untuk memunculkan sebagian tulisan atau teks paragraf, saat membuat sebuah artikel yang cukup panjang.

Caranya tidak terlalu sulit, kita bisa menggunakan jQuery plugin. Kalian bisa download disini dan disini. Untuk plugin yang teman-teman download itu Plugin utama jQuery.

Untuk source codenya, sebagai berikut:

<!DOCTYPE html>
<html>
<head>
	<title>Read More With jQuery</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			var readMoreHTML = $(".read-more").html();
			var lessText = readMoreHTML.substr(0, 100);

			if(readMoreHTML.length > 100){
				$(".read-more").html(lessText).append("<a href='' class='read-more-link'> Show More</a>");
			} else {
				$("read-more").html(readMoreHTML);
			}

			$("body").on("click", ".read-more-link", function(event){
				event.preventDefault();
				$(this).parent(".read-more").html(readMoreHTML).append("<a href='' class='show-less-link'> Show Less</a>");
			});

			$("body").on("click", ".show-less-link", function(){
				event.preventDefault();
				$(this).parent(".read-more").html(readMoreHTML.substr(0, 100)).append("<a href='' class='read-more-link'> Show More</a>");
			});
		});
	</script>
	<style type="text/css">
		body{
			background: grey;
			padding: 0;
			margin: 0;
			font-family: sans-serif;
		}

		.content{
			background: #fff;
			margin: 0 auto;
			width: 600px;
			padding: 10px;
			min-height: 300px;
		}

		.read-more{
			border: 1px solid #000;
			padding: 15px;
			margin-bottom: 20px;
		}

		.read-more-link, .show-less-link{
			text-decoration: none;
			font-weight: bold;
			color: green;
		}
	</style>
</head>
<body>
	<div class="content">
		<div class="read-more">
			There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
		</div>

		<div class="read-more">
			There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
		</div>

		<div class="read-more">
			There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
		</div>
	</div>



</body>
</html>

Untuk bagian headnya, disini kita letakkan plugin jQuery dan Aktivasinya, lihat potongan source code berikut:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			var readMoreHTML = $(".read-more").html();
			var lessText = readMoreHTML.substr(0, 100);

			if(readMoreHTML.length > 100){
				$(".read-more").html(lessText).append("<a href='' class='read-more-link'> Show More</a>");
			} else {
				$("read-more").html(readMoreHTML);
			}

			$("body").on("click", ".read-more-link", function(event){
				event.preventDefault();
				$(this).parent(".read-more").html(readMoreHTML).append("<a href='' class='show-less-link'> Show Less</a>");
			});

			$("body").on("click", ".show-less-link", function(){
				event.preventDefault();
				$(this).parent(".read-more").html(readMoreHTML.substr(0, 100)).append("<a href='' class='read-more-link'> Show More</a>");
			});
		});
	</script>

jangan lupa itu skrip diatas yang paling penting, disini kita menggunakan teknik CSS Embededd, supaya tidak terlalu banyak file.

Untuk demonya kalian bisa lihat disini:
URL:http://www.yellowweb.id/demo/read-more/

Comments are closed.