"“聪明”;HTML中的溢出:有什么方法可以放省略号“"结尾有一个链接,可以查看整个内容
"Smart" overflow in HTML: is there any way to put ellipsis "..." with a link at the end to view entire content?
我有一个大小受限的<div>
,我想在其中放入多行文本,但如果它要溢出,我希望在末尾放一个"…",并带有一个链接,以便在另一个页面上查看整个内容。
这在Javascript/CSS中可行吗?我试着搜索了一下,但不知道该找什么。
嗯——看起来有一个CSS text-overflow: ellipsis;
,但我不认为我可以在省略号上放一个链接。
这个答案非常接近,但在某些情况下,如果它刚刚开始溢出,那么只有省略号的一部分出现。
库需求:我可以使用jQuery(有点不情愿),更喜欢无框架依赖的跨浏览器解决方案。
下面是一个基本示例,它迭代具有.smart-overflow
类的元素。它添加了一个可点击的a
元素,如果内容被剪切,则该元素的类仅为ellipsis-link
。该链接有一个点击事件监听器,它将显示用overflow: hidden
隐藏的隐藏内容。此示例仅适用于单行。请参阅下面的替代示例,以获得适用于支持的浏览器中的多行的方法。
var elements = document.querySelectorAll('.smart-overflow');
Array.prototype.forEach.call(elements, function (el) {
var link = document.createElement('a');
link.href = '#'; link.className = 'ellipsis-link';
if (el.offsetWidth < el.scrollWidth) {
link.addEventListener('click', function (e) {
e.preventDefault();
el.classList.remove('smart-overflow');
});
el.appendChild(link);
}
});
p {
width: 200px;
position: relative;
}
.smart-overflow {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
a.ellipsis-link {
display: none;
}
.smart-overflow a.ellipsis-link {
display: block;
position: absolute;
right: 0; bottom: 0;
width: 1.2em;
height: 100%;
cursor: pointer;
}
<p class="smart-overflow">No ellipsis.</p>
<p class="smart-overflow">This is a longer string of text which should have ellipsis. This is a longer string of text which should have ellipsis.</p>
<p class="smart-overflow">Another string of text which should have ellipsis. Another string of text which should have ellipsis.</p>
在上面的例子中,text-overflow: ellipsis
需要white-space: nowrap
才能工作,这意味着它只适用于单行。
如果你想支持多行,你可以在支持的浏览器中这样做。如果这不起作用,请参阅下面的jQuery解决方案以获得完整的浏览器支持。
var elements = document.querySelectorAll('.smart-overflow');
Array.prototype.forEach.call(elements, function (el) {
var link = document.createElement('a');
link.href = '#'; link.className = 'ellipsis-link';
link.addEventListener('click', function (e) {
e.preventDefault();
el.classList.remove('smart-overflow');
});
el.appendChild(link);
});
p {
width: 200px;
position: relative;
}
.smart-overflow {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
max-height: 2.2em;
overflow: hidden;
position: relative;
}
a.ellipsis-link {
display: none;
}
.smart-overflow a.ellipsis-link {
display: block;
position: absolute;
right: 0; bottom: 0;
width: 4.2em;
height: 1.2em;
cursor: pointer;
}
<p class="smart-overflow">No ellipsis.</p>
<p class="smart-overflow">This is a longer multi-line string of text which should have ellipsis. This is a longer string of text which should have ellipsis.</p>
<p class="smart-overflow">Another multi-line string of text which should have ellipsis. Another multi-line string of text which should have ellipsis.</p>
jQuery多行替代方案,使用此库提供完全的浏览器支持。
$('.smart-overflow').dotdotdot({
ellipsis: '',
wrap: 'word',
callback: function(isTruncated, content) {
var self = this;
if (isTruncated) {
$(this).append($('<a/>', {
class: 'ellipsis-link',
text: '...',
href: '#',
click: function(e) {
e.preventDefault();
$(this).remove();
$(self).removeClass('smart-overflow is-truncated').trigger("destroy");
}
}));
}
}
});
p { width: 200px; }
.smart-overflow { max-height: 2.8em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.dotdotdot/1.7.4/jquery.dotdotdot.min.js"></script>
<p class="smart-overflow">No ellipsis.</p>
<p class="smart-overflow">This is a longer multi-line string of text which should have ellipsis. This is a longer string of text which should have ellipsis.</p>
<p class="smart-overflow">Another multi-line string of text which should have ellipsis. Another multi-line string of text which should have ellipsis.</p>
看看这些jQuery插件:
https://github.com/jjenzz/jquery.ellipsishttps://github.com/rviscomi/trunk8
下面是我不久前写的一些代码,它包装了trunk8,使链接中的省略号,并使它们也具有响应性。
(function($, window, document, undefined) {
'use strict';
window.seeMore = function() {
function addSeeMoreLinks() {
$article.find('p').trunk8(options).each(function() {
var $this = $(this);
if (0 === $this.find('.trunk8').length) {
$this.append(' <a href="#" class="seeMore">see more</a>.');
}
});
}
function removeSeeMoreLinks() {
$article.find('p').each(function() {
$(this).find('.seeMore').remove();
});
}
function setupSeeMoreLinks() {
addSeeMoreLinks();
$(window).resize(function() {
removeSeeMoreLinks();
addSeeMoreLinks();
});
}
var
$article = $('.blogArticleList article'),
options = {
lines: 6,
fill: '… <a href="#" class="trunk8">see more</a>.',
tooltip: false
};
setupSeeMoreLinks();
};
if (window.addEventListener && $().trunk8) {
window.seeMore();
}
})(jQuery, window, document);
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在只能有一个asp.net表单的主页上从asp.net页面中的javascript中获取值
- 有可能在来自链接的警报中有一个值吗
- 是否<asp:文本框>有一个onFocusLost事件
- 让谷歌数据图表有一个24小时x轴
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 我确实有一个表单,如果用户输入了输入,它应该检查否定或空的输入框,并抛出一条警告消息
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- 当知道同一hiearch中至少有一个元素时,遍历到元素.结构使用jquery
- PNG图像被覆盖,但每个图像都有一个链接可以更改,每次一个-Javascript/jQuery/CSS
- JQuery是否有一个“;移动“;作用或者有没有一种更紧凑的方法来做到这一点
- Regex或javascript每X个连续单词中有一个单词
- Jquery时间启动计时器,我有一个计时器的代码,但它在页面加载时启动
- 是否有一个javascript库来解析简单的查询
- 在一个函数中,我有一个未捕获的语法错误:意外的标记else
- 检查一个元素是否有一个类与另一个类总是返回true
- 我有一个用幻灯片制作的网页,但幻灯片会在屏幕上移动
- "“聪明”;HTML中的溢出:有什么方法可以放省略号“"结尾有一个链接,可以查看整个内容
- 我有一个onload=“;setDate()"在<身体>html文档的标记.为什么它没有用我的Js
- JS:有一个新的运算符"=>&”;