how to push [read more] button twice - html

I want to shrink my paragraph into a couple of read more, without the need to read less.
I only manage to do it once and 'm now lost.
.bio {
font-family: monospace;
}
#check {
display: none;
}
#check:checked~.more {
display: block;
}
.more {
display: none;
}
label {
color: rgb(0, 0, 0);
cursor: pointer;
font-weight: bold;
text-decoration: underline;
}
<div class="bio">
<input type="checkbox" id="check">
<p>craftsman</p> *original display*
<div class="more">
<p>since birth has always had a love/hate relationship</p> *first readmore*
</div>
<label for="check"> more</label>
<div class="more">
<p>She had half-survived blahblahblah.</p> *second readmore*
</div>
</div>

Make all elements with the class more hidden by using: .more { display: none }
Create a class to make elements visible again: .d-block { display: block }
Create a Node List in JS with all elements with the class more: let bio_sections = document.querySelectorAll('.more')
Create a variable at 0 as index: let bio_index = 0
Add a click-eventListener linked to the button: button-element.addEventListener('click', () => { ... })
Within the function create an if-condition to ensure that the actual function is only as often run as you have elements with the class more as otherwise, you will create an error after every further button click: if (bio_index < bio_sections.length)
select the element out of the Node List with the current index: bio_sections[bio_index]
Add that previously created class to that element: .classList.add('d-block')
Increase the index to jump to the next element within your Node List: bio_index++
let button = document.querySelector('.bio button');
let bio_sections = document.querySelectorAll('.bio .more');
/* index to iterate */
let bio_index = 0;
//trigger to check for button clicks
button.addEventListener('click', function() {
// ensures JS errors as otherwise elements going to be called that does not exist
if (bio_index < bio_sections.length) {
// makes the section visible
bio_sections[bio_index].classList.add('d-block');
//increases the index onclick
bio_index++;
}
})
.more {
display: none;
}
.d-block {
display: block;
}
button {
display: block;
margin-top: 2em;
}
<div class="bio">
<p>craftsman</p> *original display*
<div class="more">
<p>since birth has always had a love/hate relationship</p> *first readmore*
</div>
<div class="more">
<p>She had half-survived blahblahblah.</p> *second readmore*
</div>
<button>Read more about craftsman</button>
</div>

Related

how to have a dropdown activate when input is focused?

I'm trying to make a reddit clone MERN and need a dropdown like the dorpdown in the reddit search bar
the styling is not an issue the issue is to make the dropdown appear when the input bar is focused
also i am using tailwindcss
can anyone please tell me how to do this?
I suppose you want to show the select not just when you focus the input but also when you are interacting with the select: you may avoid JS at all and use the :focus-within pseudoclass
.dropdown select {
display: none;
}
.dropdown:focus-within select {
display: block;
}
<div class="dropdown">
<input />
<select>
<option>option</option>
</select>
</div>
but if you need this to be working only on input focus
.dropdown select {
display: none;
}
.dropdown input:focus + select {
display: block;
}
<div class="dropdown">
<input />
<select>
<option>option</option>
</select>
</div>
const show = () => {
document.querySelector('.content').classList.add("active")
}
const hide = () => {
document.querySelector('.content').classList.remove("active")
}
.dropdown{
display: flex;
flex-direction: column;
position: relative;
width: 200px;
}
input{
width: 100%;
}
.content{
background: red;
padding: 4px;
position: absolute;
top: 100%;
width: 100%;
display: none;
}
.active{
display: flex;
}
<div class = "dropdown">
<input onFocus = "show()" onBlur = "hide()" />
<div class = "content">
Dropdown
</div>
</div>
React and Tailwind
const Dropdown = () => {
const [isFocus, setIsFocus] = React.useState(false);
return (
<div className = "w-40 border m-4">
<input
className = "w-full"
onFocus={() => setIsFocus(true)}
onBlur={() => setIsFocus(false)}
/>
{ isFocus && <div className = "bg-red-300">Dropdown</div>}
</div>
);
};

How to highlight and get element of html element when mouse hover in vue

This is a simple html for the navigation header which I use vue to show it up with v-html.
this is the demo - https://shuffle.dev/preview?project=12e60d2546392911632c3b5d1576766424f55da0&page=index.html&screen=main
<section class="text-gray-700 font-heading font-medium relative bg-gray-50 bg-opacity-50">
<nav class="flex justify-between px-6 lg:px-12 py-8">
.......
.......
<div class="mt-auto px-10">
<button class="py-3 px-5 mt-6 w-full font-body font-bold uppercase tracking-wide text-sm border-2 border-gray-200 hover:border-gray-300 border-opacity-50 rounded-full">
<span class="block mt-px">New project</span>
</button>
</div>
</nav>
</div>
</section>
what I want to do is to have the mouse hover on each html element such as <svg>, <button>, <div>, highlight the element and output the html element in the console.
the wishful result will be similar to the image below, a highlight on the 'New Tools' text.
One simple solution is to give all the elements a class and add the mouseover listener, however it raised a issue that for a complex html element with parent and child element, when the mouse is hovering over the child element, both parent and child element will be effected, how to only highlight the child element while avoid highlighting the parent element is what I am trying to figure out.
You can add eventListener on the buttons. With CSS you can style only the span child element. The Event Handler can console what ever you want.
<!-- Use preprocessors via the lang attribute! e.g. <template lang="pug"> -->
<template>
<div id="app">
<section class="text-gray-700 font-heading font-medium relative bg-gray-50 bg-opacity-50">
<nav class="flex justify-between px-6 lg:px-12 py-8">
<div class="mt-auto px-10">
<button class="py-3 px-5 mt-6 w-full font-body font-bold uppercase tracking-wide text-sm border-2 border-gray-200 hover:border-gray-300 border-opacity-50 rounded-full">
<span class="highlight block mt-px">New project</span>
</button>
<button class="py-3 px-5 mt-6 w-full font-body font-bold uppercase tracking-wide text-sm border-2 border-gray-200 hover:border-gray-300 border-opacity-50 rounded-full">
<span class="block mt-px">New tools</span>
</button>
</div>
</nav>
</section>
</div>
</template>
<script>
export default {
data() {
return {
};
},
methods: {
},
mounted() {
//const btns = document.querySelectorAll('button');
const btns = document.querySelectorAll('button');
btns.forEach(b => {
b.addEventListener('mouseover', (e) => {
let span = e.target.children[0];
console.log('child Element from hovering button', span);
})
})
console.log(btns)
}
};
</script>
<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
a,
button {
color: #4fc08d;
}
button:hover .highlight {
background: red;
}
button {
background: none;
border: solid 1px;
border-radius: 2em;
font: inherit;
padding: 0.75em 2em;
}
</style>
Solution:
Use the mounted hook of Vue component to fetch the dynamic HTML subtree then add 'Click' event handler for eligible node elements of the subtree to trigger element selection.
Additionally use mouseover & mouseout event Handler to toggle interactive styling
Step 1: Setup the project with a vue component using v-html
<div id="app">
Create designs using Uinel
<div id="interactivecanvas" v-html="dynamicHTML"></div>
<div id="selectedElement">Selection: {{selectedElement.tagName}}</div>
</div>
Step 2: Add CSS styles to indicate a Selectable Element
.selectable{
box-shadow: 0 0 0 2px green;
}
Step 3: Add a mounted hook to fetch dynamic HTML Sub-tree
mounted(){
// List of selectable Elements. Uses css selector syntax
let selectableElements = ['li', 'span', 'button']
selectableElements[0] = '#interactivecanvas '+selectableElements[0]
let canvasContents = document.querySelectorAll(selectableElements.join(',
#interactivecanvas '))
.....
Step 4: Iterate through Sub-tree Elements and add Event Handlers for Selection
mounted(){
...
for(let i=0; i<canvasContents.length; i++){
...
canvasContents[i].addEventListener('click',e=>{
e.stopPropagation();
this.selectElement(e)
})
...
}
Step 5: Optional! you can add mouseover & mouseout event Handler to toggle interactive styling.
Check the example below. View in full page.
new Vue({
el: '#app',
data() {
return {
selectedElement: {},
dynamicHTML: `
<div class="header">
<nav class="nav">
<ul id="nav">
<li>Home</li>
<li>Contact Us</li>
<li>About Us</li>
</ul>
<div id="card">
<div id="usercard">
<span>Sona</span>
<span>▼</span>
</div>
<button>New Project</button>
</div>
<nav>
</div>
`
}
},
mounted(){
// List of selectable Elements. Uses css selector syntax
let selectableElements = ['li', 'span', 'button']
selectableElements[0] = '#interactivecanvas '+selectableElements[0]
let canvasContents = document.querySelectorAll(selectableElements.join(', #interactivecanvas '))
for(let i=0; i<canvasContents.length; i++){
// Add mouseover & mouseout event Handler to toggle styles
canvasContents[i].addEventListener('mouseover',(e)=>{
e.stopPropagation();
e.currentTarget.classList.add("selectable")
})
canvasContents[i].addEventListener('mouseout',(e)=>{
e.stopPropagation();
e.currentTarget.classList.remove("selectable")
})
// Event to fire Selection action. Use 'hover' or 'click'
canvasContents[i].addEventListener('click',e=>{
e.stopPropagation();
this.selectElement(e)
})
}
},
methods:{
selectElement(e){
console.log('Selected '+e.currentTarget)
this.selectedElement = e.currentTarget
}
}
})
* {
box-sizing: border-box;
}
body {
font-family: 'Verdana'
}
ul {
list-style: none;
}
.header {
background: rgba(245,246,247);
padding: 20px;
}
.nav {
display: flex;
}
.nav > ul > li {
display: inline;
margin: 20px;
}
.nav button {
border: 2px solid gray;
border-radius: 9999px;
padding: 12px 20px;
}
.nav #card {
display: flex;
align-items: center;
}
#usercard {
margin: 20px;
padding: 10px;
}
.selectable{
box-shadow: 0 0 0 2px green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
Create designs using Uinel
<div id="selectedElement">Selection: {{selectedElement.tagName}}</div>
<div id="interactivecanvas" v-html="dynamicHTML"></div>
</div>
You can use the /deep/ selector to get your styles to be applied:
<div class="description" v-if="description" v-html="description"></div>
.description {
/deep/ p {
margin-bottom: 10px;
}
}
You could create a highlighted class like this:
.highlighted { border: 1px dashed green; }
and then add 2 functions to your component:
highlight(event) {
console.log(event.target);
event.target.classList.add("highlighted");
},
removeHighlight(event) {
event.target.classList.remove("highlighted");
},
and add these attributes to the element you want to highlight and output to the console:
#mouseenter="highlight($event)" #mouseleave="removeHighlight($event)"

How do I hover over span to let a div appear?

#hello{
font-size: 4em;
}
div.about{
display: none;
}
#hello:hover div.about {
display: block;
}
<pre id="hometext"><span id="hello">Hello!</span></pre>
<div class="about" id="about"><p>hello</p></div>
First of all, I am new to stackoverflow. Secondly, I want to over a specific part of a paragraph, the span, and then let this div appear. But it doesnt seem to work..
You dont have to use javascript:
#hometext:hover + #about { display:none; }
I am not quite sure if this is what you asked for, but you can utilize the span element's onmouseover and onmouseout attributes.
With a little bit of javascript, you can achieve what I think you want to do:
function hideDiv() {
document.getElementById("divToHide").style.visibility = 'hidden';
}
function showDiv() {
document.getElementById("divToHide").style.visibility = 'visible';
}
#divToHide {
height: 100px;
width: 100px;
background: red;
}
#hoverMe {
cursor: pointer;
}
<div id="divToHide">
</div>
<br />
<p>
This is a paragraph. If you hover <span id="hoverMe" onmouseover="hideDiv()" onmouseout="showDiv()">here</span>, it will hide the red box.
</p>
I think you need some javascript there:
function showOtherDiv() {
document.getElementById("about").style.display = "block";
}
function hideOtherDiv() {
document.getElementById("about").style.display = "none";
}
#hello {
font-size: 4em;
}
div.about {
display: none;
}
#hello:hover div.about {
display: block;
}
<pre id="hometext">
<span id="hello" onmouseover="showOtherDiv()" onmouseout="hideOtherDiv()">Hello!</span>
</pre>
<div class="about" id="about">
<p>hello</p>
</div>
Here is a codepen

WinJS.BackButton sizes

I have this html tag which reffers to the backButton provided by the WinJS library:
<button data-win-control="WinJS.UI.BackButton"></button>
I want to change its size. How can I do that? I tried using CSS by adding the ID "backButton" and font-size OR width/height properties, like this:
#backButton {
font-size: small;
}
#backButton {
height: 30px;
width: 30px;
}
EDIT: Code added and a picture of what happens when changing the values of width/height of the button.
// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
"use strict";
WinJS.UI.Pages.define("/pages/anime/anime.html", {
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
// TODO: Initialize the page here.
this.renderAnimeInfo(Identifier.file);
},
unload: function () {
// TODO: Respond to navigations away from this page.
},
updateLayout: function (element) {
/// <param name="element" domElement="true" />
// TODO: Respond to changes in layout.
},
renderAnimeInfo: function (id) {
// Path for the anime data.
var path = "data/animes.json";
// Retrieve the .json.
WinJS.xhr({ url: path }).then(
function (response) {
var json = JSON.parse(response.responseText);
for (var i = 0; i < json.length; i++) {
if (json[i].file == id) {
var animeData = json[i];
break;
}
}
},
function (error) {},
function (progress) {}
);
},
});
})();
.right {
float: right;
}
.left {
float: left;
}
.active {
background-color: blue;
}
#animeDetails {
background: red;
height: 100%;
width: 300px;
float: left;
}
#animeInfo {
display: -ms-grid;
height: 100%;
width: calc(100% - 300px);
float: right;
}
#navbar {
-ms-grid-row: 1;
padding: 20px 25px;
}
#navbar .right button {
margin-right: 4px;
}
#navbar input {
width: 150px;
}
#details {
-ms-grid-row: 2;
padding: 0 25px;
text-align: justify;
white-space: pre-line;
}
#details h3 {
width: 100%;
padding: 5px 0;
border-bottom: 1px solid #bebebe;
margin-bottom: 0;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>anime</title>
<link href="anime.css" rel="stylesheet" />
<script src="anime.js"></script>
</head>
<body>
<div id="animeDetails"></div>
<div id="animeInfo">
<div id="navbar">
<div class="left">
<button class="left" data-win-control="WinJS.UI.BackButton"></button>
<h3>Back</h3>
</div>
<div class="right">
<button type="button" class="active">Details</button>
<button type="button">Episodes</button>
<button type="button">Characters</button>
<button type="button">Staff</button>
<input type="search" placeholder="Search" />
</div>
</div>
<div id="details">
<div id="synopsis">
<h3>Synopsis</h3>
<span>
</span>
</div>
</div>
</div>
</body>
When using the width/height properties, what happens is that the button does resize to the specified value, but the icon inside (which is not a background) doesn't. http://i.imgur.com/lMqmL0G.png
Possibly you have to set display: inline-block to button because the width of an element with display: inline (the default for buttons) is exactly the same as its content because it only takes up the space needed to display its contents so try with:
With id selector
#backButton {
height: 30px;
width: 30px;
display: inline-block;
}
<button id="backButton" data-win-control="WinJS.UI.BackButton"></button>
With style inline
<button data-win-control="WinJS.UI.BackButton" style="width: 30px; height: 30px; display: inline-block"></button>
Try to set the styles to child element .win-back
#backButton .win-back{
/*---styles---*/
}
You haven't given your button an ID. The CSS does not know what tag to link to.
<button id="backButton" data-win-control="WinJS.UI.BackButton"></button>
edit: you may find the following reference useful CSS Selectors

How to make DIV constantly display text on click?

I want a way (ideally just using CSS HTML) where when I click on Question, the text shows and remains there. Then if they click on text again, it will revert back to question.
It works on hover, but I don't know how to make it on click. HTML:
<div id="packagequestioninfo">
<p class="packagereplies">Question</p>
<p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>
CSS:
#packagequestioninfo {
padding: 10px;
background: #F2F7FA;
border-radius: 0px;
-moz-box-shadow: 0 0 5px #ccc;
-webkit-box-shadow: 0 0 5px #ccc;
box-shadow: 0 0 5px #ccc;
}
#packagequestioninfo:hover {
background-image:url(../img/index/body/ourproducts/light_blue_background_pattern.jpg);
background-repeat:repeat;
cursor: none;
}
#packagequestioninfo .packagecomment {
display: none;
}
#packagequestioninfo:hover .packagereplies {
display: none;
}
#packagequestioninfo:hover .packagecomment {
display: inline;
}
Here is the code http://jsfiddle.net/53UK2/
Make .packagecomment invinsible with css, then use jQuery:
$('p').click(function(){
$('p').toggle();
});
Fiddle: http://fiddle.jshell.net/RcTGN/
If you do not need IE8 and lower support you can do it with pure CSS like this.
This is because of the ":checked" css selector. See support here.
The HTML:
<div class="question">
<input type="checkbox" class="qestion-checkbox" id="q1" />
<label for="q1" class="qestion-text">Question 1 text</label>
<label for="q1" class="qestion-answer">Question 1 answer</label>
</div>
<div class="question">
<input type="checkbox" class="qestion-checkbox" id="q2" />
<label for="q2" class="qestion-text">Question 2 text</label>
<label for="q2" class="qestion-answer">Question 2 answer</label>
</div>
The CSS:
.qestion-answer, .qestion-checkbox {
display: none;
}
.qestion-checkbox:checked + .qestion-text {
display:none;
}
.qestion-checkbox:checked + .qestion-text + .qestion-answer {
display:block;
}
If you do need IE8 and lower support you need to use some javascript/jQuery
It's impossible using only css. Of course you can change :hover instead :active, but it will be work when the mouse button is held down.
Check it.
Why don't you want use jquery? It will be easier.
Non-jquery way...
Just showing and hiding by changing the display style attribute:
HTML:
<script src="javascriptfile.js"></script>
<div id="packagequestioninfo">
<p class="packagereplies">Question</p>
<p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>
javascriptfile.js:
// Wait for the entire window elements to be loaded
window.onload = function() {
document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {
if(document.getElementById('packagecomment').style.display !== 'none') {
document.getElementById('packagecomment').style.display = 'none';
} else {
document.getElementById('packagecomment').style.display = 'block';
}
});
}
or if you'd like to go with the class toggling method:
window.onload = function() {
document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {
if(document.getElementById('packagecomment').className.indexOf('show')) {
document.getElementById('packagecomment').className = '';
} else {
document.getElementById('packagecomment').className = 'show';
}
});
}
in this approach you'll need to add a class to your CSS:
.show{
display:block;
}