Method of skipping text beyond the set number of lines

Source: Internet
Author: User

Because most of the mobile is the WebKit kernel browser, if the pure to achieve the text beyond the specified number of lines, the rest of the content omitted and added "...", you can directly use the WebKit private properties:-webkit-line-clamp来实现

CSS Code

. Box {    : hidden;     text-overflow: ellipsis;     display: -webkit-box;     -webkit-line-clamp: 2;     -webkit-box-orient: vertical;}

HTML code

<class= "box">The completion of the 001A aircraft carrier marks China as one of the few countries in the world where aircraft carriers can be built independently, As China is in the process of rapid development of the Blue Water Navy, in the near future China will have more than one aircraft carrier formation can be expected, China's construction of a strong navy has really opened the curtain. </ Div >

If there is still a need to expand all and the action, then the light with CSS is not possible.

A more mature JQuery plugin is recommended here-jquery.dotdotdot

First introduce Jquery.js and dotdotdot.js

HTML code

<class= "Dotbox"> The completion of the 001A aircraft carrier marks China as one of the few countries in the world where aircraft carriers can be built independently, As China is in the process of rapid development of the Blue Water Navy, in the near future China will have more than one aircraft carrier formation can be expected, China's construction of a strong navy has really opened the curtain.  </div>

CSS Code

. dotbox.opened{Height:Auto;} /* Expand Container height adaptive */. Dotbox. Close,.dotbox. open,.dotbox.opened. Open{Display:None;} /* Default Expand Hide button Hidden, expand when expand button hide */. dotbox.is-truncated. open,.dotbox.opened. Close{Display:Inline-block;} /* Out of the specified number of lines expand button Display, expand after button display */. Dotbox {Line-height:18px;Height:36px;Color:#333;}. More-btn{Display:Block;Color:#333;Line-height:18px;}
. Toggle{display:block;}

JS Code

<script type= "Text/javascript" >
$ (function () {
var $dot = $ ('.Dotbox‘);
$dot. Append (' <a class= ' toggle "><span class=" more-btn open "> View all </span><span class=" more-btn Close > Collect </span></a> ');
function Createdots () {
$dot. Dotdotdot ({
After: ' A.toggle '
});
}

function Destroydots () {
$dot. Trigger (' destroy ');
}
Createdots ();
$dot. On (
' Click ',
' A.toggle ',
function () {
$dot. Toggleclass (' opened ');
if ($dot. Hasclass (' opened ')) {
Destroydots ();
} else {
Createdots ();
}
return false;
}
);
})
</script>

Method of skipping text beyond the set number of lines

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.