Home Web Front-end JS Tutorial Example analysis of super simple like effect implemented by jQuery_jquery

Example analysis of super simple like effect implemented by jQuery_jquery

May 16, 2016 pm 03:22 PM
jquery like

The example in this article describes the super simple like effect implemented by jQuery. It is shared with everyone for your reference. The details are as follows:

1.HTML (can be optimized to have as few tags as possible...)

<div id="dianz">
  <b class="cz"><em>1</em><i></i><s></s><u>超赞</u></b>
  <b class="tj"><em>2</em><i></i><s></s><u>推荐</u></b>
  <b class="yb"><em>3</em><i></i><s></s><u>一般</u></b>
  <b class="wl"><em>6</em><i></i><s></s><u>无聊</u></b>
  <b class="lj"><em>5</em><i></i><s></s><u>雷囧</u></b>
</div>

Copy after login

2.css style

#dianz{text-align:center; width:610px; overflow:hidden;zoom:1; margin:20px auto;}
#dianz b{ display:inline-block; width:120px; height:215px; float:left; position:relative;}
#dianz b em,#dianz b u,#dianz b i,#dianz b s{display:inline-block; width:100%; height:20px; position:absolute; left:0px;}
#dianz b u{ bottom:0px;}
#dianz b s{ bottom:20px; height:95px;}
#dianz b i{width:20px; height:80px;left:50px; bottom:115px;}
#dianz b.cz s{ background:url(../images/dianz.jpg) 25px 0px no-repeat}
#dianz b.cz i{ background-color:#fe0032;}
#dianz b.tj s{ background:url(../images/dianz.jpg) -105px 0px no-repeat}
#dianz b.tj i{ background-color:#fe9903;}
#dianz b.yb s{ background:url(../images/dianz.jpg) -235px 0px no-repeat}
#dianz b.yb i{ background-color:#99c900;}
#dianz b.wl s{ background:url(../images/dianz.jpg) -370px 0px no-repeat}
#dianz b.wl i{ background-color:#32ccff;}
#dianz b.lj s{ background:url(../images/dianz.jpg) -500px 0px no-repeat}
#dianz b.lj i{ background-color:#3167ff;}

Copy after login

3.js (the use of js is not very good, everyone can optimize it better)

function o_dianz(){
  var oi=$('#dianz b i'); //获取i;
    oem=$('#dianz b em'); //获取em;
    os=$('#dianz b s');//获取s;
    bl=null;
    osz=null;
    Arr=[];
    Arr2=[];
 function o_mm(){
  oem.each(function(){
      osz=$(this).text();
      Arr.push(osz);
      //console.log(Arr)
     });
  var get_max=Math.max.apply(null,Arr); //获取最大点赞数;
  bl=80/get_max;
  oem.each(function(){
      osz=$(this).text();
      var oi_H=Math.floor(osz*bl);
      Arr2.push(oi_H);
     });
  for(var i=0; i<Arr2.length; i++){
      oi.eq(i).height(Arr2[i]);
      oem.eq(i).css('top',80-Arr2[i]);
    };
   };
   o_mm();
  os.click(function(){ //点赞增加;
      Arr=[];
      Arr2=[];
      osz=$(this).siblings('em').text();
      osz++;
      $(this).siblings('em').text(osz);
      o_mm();
    });
  };
o_dianz();

Copy after login

Okay, the code is posted, it’s super simple. The principle I wrote (I don’t know if there is a better one, and the bug has not been detected): Set the default height of the i tag to 80px, then find the text value of each em through js, throw it into the array Arr, and then pass This method Math.max.apply(null,Arr), finds the value of the maximum text, and then finds the scale (finding the scale through the maximum text can ensure that the height will not be greater than 80PX), and finally multiplies the value of each text by the scale Math .floor(osz*bl), find the height value corresponding to each em.

Every time the icon is clicked in the subsequent click event, Arr and Arr2 are reset accordingly, which ensures that the values ​​are updated in real time. . . OK, it’s over here,

Final result:

I hope this article will be helpful to everyone in jQuery programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between likes and love on a WeChat video account? What is the use? What is the difference between likes and love on a WeChat video account? What is the use? Mar 26, 2024 pm 12:16 PM

In WeChat video accounts, likes and love are common ways for users to interact. Likes indicate the user's recognition and support for the video content they like, while hearts show the user's love and concern for the video creator. Although likes and hearts both express the user's recognition and love for the video content, there are some subtle differences in substance. 1. What is the difference between likes and love on a WeChat video account? Likes are often a way of expressing general approval and support, with users simply clicking the &quot;Like&quot; button under a video to express their approval of the video. This simple and direct format allows users to express their opinions quickly, while also helping the video spread more widely. Love is personal and emotional. In the WeChat video account, users can click the &quot;

Where can I read the comments liked by Xiaohongshu? What rules do you need to pay attention to on the Xiaohongshu platform? Where can I read the comments liked by Xiaohongshu? What rules do you need to pay attention to on the Xiaohongshu platform? Mar 30, 2024 pm 06:51 PM

In today's era of information explosion, social media has become an indispensable part of people's daily lives. As the leading lifestyle sharing platform in China, Xiaohongshu has attracted a large number of users to share and obtain information here. So, how do we view like comments on the Xiaohongshu platform? This article will answer this question in detail. 1. Where can I read the comments liked by Xiaohongshu? First, we need to log in to our account on the Xiaohongshu platform. After logging in, we can see the content recommended on the homepage and the updates posted by people we follow. Below each post, we can see buttons for like and comment. Click the Like button to express your approval of this content. Click the comment button to view other users' comments on the content. 3.If

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

What do you think about other people's comments that Xiaohongshu has liked? Which types of content are more likely to get likes and comments? What do you think about other people's comments that Xiaohongshu has liked? Which types of content are more likely to get likes and comments? Mar 30, 2024 pm 08:06 PM

In the digital age, Xiaohongshu has become an important platform for people to share their lives and exchange experiences. Users interact through likes and comments, forming a tight social network. This article will explore how to view comments that have been liked by others on Xiaohongshu, and what types of content are more likely to get likes and comments. 1. What do you think about other people’s comments that Xiaohongshu has liked? Users can interact by viewing liked comments to express support and liking. The specific viewing method is as follows: 1. On the Xiaohongshu homepage, click the "Discover" button to enter the "Discover" page. 2. On the "Discover" page, click the "Circle" button to enter the "Circle" page. 3. On the "Circles" page, browse the topics or circles you are interested in and find comments you like. 4. Click on the comment to see the likes

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

See all articles