简体   繁体   English

对齐 JavaScript 阅读 div 底部的更多按钮

[英]Align JavaScript read more buttons at the bottom of the div

I created three boxes with content in this area.我在这个区域创建了三个包含内容的盒子。 To make the boxes smaller, I've included more functionality.为了使盒子更小,我添加了更多功能。 It was working great, but the buttons were not properly aligned.它工作得很好,但是按钮没有正确对齐。 Could someone possibly assist me with aligning those buttons at the bottom of the div?有人可以帮助我对齐 div 底部的那些按钮吗?

 ;(function (win, doc, undef) { 'use strict'; /** * @desc this object holds all functions */ var RM = {}; /* ============================== */ /* HELPERS */ /* ============================== */ RM.helpers = { extendObj: function() { for (var i = 1, l = arguments.length; i < l; i++) { for (var key in arguments[i]) { if (arguments[i].hasOwnProperty(key)) { if (arguments[i][key] && arguments[i][key].constructor && arguments[i][key].constructor === Object) { arguments[0][key] = arguments[0][key] || {}; this.extendObj(arguments[0][key], arguments[i][key]); } else { arguments[0][key] = arguments[i][key]; } } } } return arguments[0]; } }; /* ============================== */ /* MAIN FUNCTIONS */ /* ============================== */ // Return the number of words of string. RM.countWords = function (str) { return str.split(/\s+/).length; }; // Rturn string starting from first word untill number specified. RM.generateTrimmed = function (str, wordsNum) { return str.split(/\s+/).slice(0, wordsNum).join(' ') + '...'; }; // Plugin Initialization RM.init = function (options) { var defaults = { target: '', numOfWords: 50, toggle: true, moreLink: 'Read more...', lessLink: 'Read less', linkClass: 'rm-link', containerClass: false }; options = RM.helpers.extendObj({}, defaults, options); var target = doc.querySelectorAll(options.target), // Get the node list of target elements specified by the user. targetLen = target.length, // Length of the targets node list. targetContent, // The initial text that is contained in the target element. trimmedTargetContent, // The final (trimmed) text. targetContentWords, // The number of words the initial text has. initArr = [], // Array to hold the initial text of each target element. trimmedArr = [], // Array to hold the final (trimmed) text of each target element. i, j, l, moreContainer, rmLink, moreLinkID, index; // Loop through all target elements for (i = 0; i < targetLen; i++) { targetContent = target[i].innerHTML; // Get the initial text of each target element. trimmedTargetContent = RM.generateTrimmed(targetContent, options.numOfWords); // Generate the trimmed version of the initial text. targetContentWords = RM.countWords(targetContent); // Count the number of words the initial text has. initArr.push(targetContent); // Push the initial text to initArr. trimmedArr.push(trimmedTargetContent); // Push the trimmed text to trimmedArr. // Procceed only if the number of words specified by the user // is smaller than the number of words the target element has. if (options.numOfWords < targetContentWords - 1) { target[i].innerHTML = trimmedArr[i]; // Populate the target element with the trimmed version of text. moreContainer = doc.createElement('div'); // Create a div element to hold the More/Less link. if(options.containerClass) { moreContainer.className = options.containerClass; } moreContainer.innerHTML = '<a id="rm-more_'+ i + '"' // Create the More/Less link. + ' class="'+ options.linkClass +'"' + ' style="cursor:pointer;padding:10px;background-color:#86BC25;margin-left:10px;color:#fff;text-decoration:none;fonr-weight:bold;border-radius:5px;" data-readmore="anchor">' + options.moreLink + '</a>'; target[i].parentNode.insertBefore(moreContainer, target[i].nextSibling); // Insert the More/Less link after the target element. } } rmLink = doc.querySelectorAll('[data-readmore="anchor"]'); // Reference the More/Less link. // Loop through all links and attach event listeners. for (j = 0, l = rmLink.length; j < l; j++) { rmLink[j].onclick = function () { moreLinkID = this.getAttribute('id'); // Get each link's unique identifier. index = moreLinkID.split('_')[1]; // Extract index number from each link's 'id'. // if (.helpers.classList,contains(this. 'less')) { if (this.getAttribute('data-clicked');== 'true') { target[index].innerHTML = initArr[index]. if (options.toggle;== false) { this.innerHTML = options,lessLink; this.setAttribute('data-clicked'; true). } else { this;innerHTML = ''. } } else { target[index].innerHTML = trimmedArr[index]; this.innerHTML = options,moreLink; this;setAttribute('data-clicked'; false). } }; } }, // Return as global object window.$readMoreJS = RM; }(this. this:document)). $readMoreJS,init({ target: ',show_more': numOfWords, 50: toggle, true: moreLink; 'Show more content', lessLink: 'Show less content' });
 .col-container { display: flex; width: 100%; }.col { flex: 1; padding: 10px; margin: 10px;border:2px solid green; } div.text-container { margin: 0 auto; width: 75%; }.hideContent { overflow: hidden; line-height: 1em; height: 2em; }.showContent { line-height: 1em; height: auto; }.showContent{ height: auto; } h1 { font-size: 24px; } p { padding: 10px 0; }.show-more { padding: 10px 0; text-align: center; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/> <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script> <div class="col-container"> <div class="col"> <h2>Column 1</h2> <p class="show_more">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Conten </p> </div> <div class="col"> <h2>Column 2</h2> <p class="show_more">when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including</p> </div> <div class="col"> <h2>Column 3</h2> <p class="show_more">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> </div> </div>

Hello there, I created three boxes with content in this area.你好,我在这个区域创建了三个包含内容的盒子。 To make the boxes smaller, I've included more functionality.为了使盒子更小,我添加了更多功能。 It was working great, but the buttons were not properly aligned.它工作得很好,但是按钮没有正确对齐。 Could someone possibly assist me with aligning those buttons at the bottom of the div?有人可以帮助我对齐 div 底部的那些按钮吗?

You just need to display flex col itself, and apply the justify space between to let it grow with the content, think this is what you are after?您只需要显示 flex col 本身,并在其之间应用 justify 空间以使其与内容一起增长,您认为这就是您所追求的吗?

 ;(function (win, doc, undef) { 'use strict'; /** * @desc this object holds all functions */ var RM = {}; /* ============================== */ /* HELPERS */ /* ============================== */ RM.helpers = { extendObj: function() { for (var i = 1, l = arguments.length; i < l; i++) { for (var key in arguments[i]) { if (arguments[i].hasOwnProperty(key)) { if (arguments[i][key] && arguments[i][key].constructor && arguments[i][key].constructor === Object) { arguments[0][key] = arguments[0][key] || {}; this.extendObj(arguments[0][key], arguments[i][key]); } else { arguments[0][key] = arguments[i][key]; } } } } return arguments[0]; } }; /* ============================== */ /* MAIN FUNCTIONS */ /* ============================== */ // Return the number of words of string. RM.countWords = function (str) { return str.split(/\s+/).length; }; // Rturn string starting from first word untill number specified. RM.generateTrimmed = function (str, wordsNum) { return str.split(/\s+/).slice(0, wordsNum).join(' ') + '...'; }; // Plugin Initialization RM.init = function (options) { var defaults = { target: '', numOfWords: 50, toggle: true, moreLink: 'Read more...', lessLink: 'Read less', linkClass: 'rm-link', containerClass: false }; options = RM.helpers.extendObj({}, defaults, options); var target = doc.querySelectorAll(options.target), // Get the node list of target elements specified by the user. targetLen = target.length, // Length of the targets node list. targetContent, // The initial text that is contained in the target element. trimmedTargetContent, // The final (trimmed) text. targetContentWords, // The number of words the initial text has. initArr = [], // Array to hold the initial text of each target element. trimmedArr = [], // Array to hold the final (trimmed) text of each target element. i, j, l, moreContainer, rmLink, moreLinkID, index; // Loop through all target elements for (i = 0; i < targetLen; i++) { targetContent = target[i].innerHTML; // Get the initial text of each target element. trimmedTargetContent = RM.generateTrimmed(targetContent, options.numOfWords); // Generate the trimmed version of the initial text. targetContentWords = RM.countWords(targetContent); // Count the number of words the initial text has. initArr.push(targetContent); // Push the initial text to initArr. trimmedArr.push(trimmedTargetContent); // Push the trimmed text to trimmedArr. // Procceed only if the number of words specified by the user // is smaller than the number of words the target element has. if (options.numOfWords < targetContentWords - 1) { target[i].innerHTML = trimmedArr[i]; // Populate the target element with the trimmed version of text. moreContainer = doc.createElement('div'); // Create a div element to hold the More/Less link. if(options.containerClass) { moreContainer.className = options.containerClass; } moreContainer.innerHTML = '<a id="rm-more_'+ i + '"' // Create the More/Less link. + ' class="'+ options.linkClass +'"' + ' style="cursor:pointer;padding:10px;background-color:#86BC25;margin-left:10px;color:#fff;text-decoration:none;fonr-weight:bold;border-radius:5px;" data-readmore="anchor">' + options.moreLink + '</a>'; target[i].parentNode.insertBefore(moreContainer, target[i].nextSibling); // Insert the More/Less link after the target element. } } rmLink = doc.querySelectorAll('[data-readmore="anchor"]'); // Reference the More/Less link. // Loop through all links and attach event listeners. for (j = 0, l = rmLink.length; j < l; j++) { rmLink[j].onclick = function () { moreLinkID = this.getAttribute('id'); // Get each link's unique identifier. index = moreLinkID.split('_')[1]; // Extract index number from each link's 'id'. // if (.helpers.classList,contains(this. 'less')) { if (this.getAttribute('data-clicked');== 'true') { target[index].innerHTML = initArr[index]. if (options.toggle;== false) { this.innerHTML = options,lessLink; this.setAttribute('data-clicked'; true). } else { this;innerHTML = ''. } } else { target[index].innerHTML = trimmedArr[index]; this.innerHTML = options,moreLink; this;setAttribute('data-clicked'; false). } }; } }, // Return as global object window.$readMoreJS = RM; }(this. this:document)). $readMoreJS,init({ target: ',show_more': numOfWords, 50: toggle, true: moreLink; 'Show more content', lessLink: 'Show less content' });
 .col-container { display: flex; width: 100%; }.col { flex: 1; padding: 10px; display: flex; flex-direction: column; margin: 10px; justify-content: space-between; margin: 10px; border:2px solid green; } div.text-container { margin: 0 auto; width: 75%; }.hideContent { overflow: hidden; line-height: 1em; height: 2em; }.showContent { line-height: 1em; height: auto; }.showContent{ height: auto; } h1 { font-size: 24px; } p { padding: 10px 0; }.show-more { padding: 10px 0; text-align: center; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/> <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script> <div class="col-container"> <div class="col"> <h2>Column 1</h2> <p class="show_more">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Conten </p> </div> <div class="col"> <h2>Column 2</h2> <p class="show_more">when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including</p> </div> <div class="col"> <h2>Column 3</h2> <p class="show_more">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> </div> </div>

I believe playing with css position would do the trick.我相信玩 ZC7A62 8CBA22E28EB17B5F5C6AE2A266AZ position可以解决问题。

You could, for example, set a position attribute for the div you want to be the reference for positioning your buttons (I didn´t quite get if in your case it is col-container, col or.text-container):例如,您可以为要作为定位按钮参考的 div 设置 position 属性(如果在您的情况下它是 col-container、col 或.text-container,我不太明白):

div.text-container {
  position: relative;
  margin: 0 auto;
  width: 75%;    
}

And then on the buttons, add an absolute positioning, setting the displacement you want from the div's borders, using top/bottom and/or right/left.然后在按钮上,添加一个绝对定位,使用顶部/底部和/或右/左设置您想要的 div 边框的位移。 For example:例如:

.show-more {
  position: absolute;
  bottom: 0;
  right: 100px;
  padding: 10px 0;
  text-align: center;
}

Here bottom: 0 will position your buttom at exactly 0 pixels fromt he bottom of the div and 100px from the right.这里bottom: 0将 position 您的臀部在距 div 底部正好 0 像素处,距右侧 100 像素处。 Note that top/bottom and right/left are excludent.请注意,top/bottom 和 right/left 不包括在内。

Try to play around a little bit.试着玩一下。

在此处输入图像描述

Here's the reference for css positioning: https://www.w3schools.com/css/css_positioning.asp下面是css定位参考: https://www.w3schools.com/css/css_positioning.asp

You can try this你可以试试这个

 ;(function (win, doc, undef) { 'use strict'; /** * @desc this object holds all functions */ var RM = {}; /* ============================== */ /* HELPERS */ /* ============================== */ RM.helpers = { extendObj: function() { for (var i = 1, l = arguments.length; i < l; i++) { for (var key in arguments[i]) { if (arguments[i].hasOwnProperty(key)) { if (arguments[i][key] && arguments[i][key].constructor && arguments[i][key].constructor === Object) { arguments[0][key] = arguments[0][key] || {}; this.extendObj(arguments[0][key], arguments[i][key]); } else { arguments[0][key] = arguments[i][key]; } } } } return arguments[0]; } }; /* ============================== */ /* MAIN FUNCTIONS */ /* ============================== */ // Return the number of words of string. RM.countWords = function (str) { return str.split(/\s+/).length; }; // Rturn string starting from first word untill number specified. RM.generateTrimmed = function (str, wordsNum) { return str.split(/\s+/).slice(0, wordsNum).join(' ') + '...'; }; // Plugin Initialization RM.init = function (options) { var defaults = { target: '', numOfWords: 50, toggle: true, moreLink: 'Read more...', lessLink: 'Read less', linkClass: 'rm-link', containerClass: false }; options = RM.helpers.extendObj({}, defaults, options); var target = doc.querySelectorAll(options.target), // Get the node list of target elements specified by the user. targetLen = target.length, // Length of the targets node list. targetContent, // The initial text that is contained in the target element. trimmedTargetContent, // The final (trimmed) text. targetContentWords, // The number of words the initial text has. initArr = [], // Array to hold the initial text of each target element. trimmedArr = [], // Array to hold the final (trimmed) text of each target element. i, j, l, moreContainer, rmLink, moreLinkID, index; // Loop through all target elements for (i = 0; i < targetLen; i++) { targetContent = target[i].innerHTML; // Get the initial text of each target element. trimmedTargetContent = RM.generateTrimmed(targetContent, options.numOfWords); // Generate the trimmed version of the initial text. targetContentWords = RM.countWords(targetContent); // Count the number of words the initial text has. initArr.push(targetContent); // Push the initial text to initArr. trimmedArr.push(trimmedTargetContent); // Push the trimmed text to trimmedArr. // Procceed only if the number of words specified by the user // is smaller than the number of words the target element has. if (options.numOfWords < targetContentWords - 1) { target[i].innerHTML = trimmedArr[i]; // Populate the target element with the trimmed version of text. moreContainer = doc.createElement('div'); // Create a div element to hold the More/Less link. if(options.containerClass) { moreContainer.className = options.containerClass; } moreContainer.innerHTML = '<a id="rm-more_'+ i + '"' // Create the More/Less link. + ' class="'+ options.linkClass +'"' + ' style="cursor:pointer;padding:10px;background-color:#86BC25;margin-left:10px;color:#fff;text-decoration:none;fonr-weight:bold;border-radius:5px;" data-readmore="anchor">' + options.moreLink + '</a>'; target[i].parentNode.insertBefore(moreContainer, target[i].nextSibling); // Insert the More/Less link after the target element. } } rmLink = doc.querySelectorAll('[data-readmore="anchor"]'); // Reference the More/Less link. // Loop through all links and attach event listeners. for (j = 0, l = rmLink.length; j < l; j++) { rmLink[j].onclick = function () { moreLinkID = this.getAttribute('id'); // Get each link's unique identifier. index = moreLinkID.split('_')[1]; // Extract index number from each link's 'id'. // if (.helpers.classList,contains(this. 'less')) { if (this.getAttribute('data-clicked');== 'true') { target[index].innerHTML = initArr[index]. if (options.toggle;== false) { this.innerHTML = options,lessLink; this.setAttribute('data-clicked'; true). } else { this;innerHTML = ''. } } else { target[index].innerHTML = trimmedArr[index]; this.innerHTML = options,moreLink; this;setAttribute('data-clicked'; false). } }; } }, // Return as global object window.$readMoreJS = RM; }(this. this:document)). $readMoreJS,init({ target: ',show_more': numOfWords, 50: toggle, true: moreLink; 'Show more content', lessLink: 'Show less content' });
 .col-container { display: flex; width: 100%; }.col { flex: 1; padding: 10px; display: flex; flex-direction: column; margin: 10px; justify-content: space-between; margin: 10px; border:2px solid green; } div.text-container { margin: 0 auto; width: 75%; }.hideContent { overflow: hidden; line-height: 1em; height: 2em; }.showContent { line-height: 1em; height: auto; }.showContent{ height: auto; } h1 { font-size: 24px; } p { padding: 10px 0; }.show-more { padding: 10px 0; text-align: center; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/> <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script> <div class="col-container"> <div class="col"> <h2>Column 1</h2> <p class="show_more">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Conten </p> </div> <div class="col"> <h2>Column 2</h2> <p class="show_more">when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including</p> </div> <div class="col"> <h2>Column 3</h2> <p class="show_more">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM