From this example, I am trying to dim (i.e. reduce opacity) non-hover elements of a html page.
The code below works only for non-overlapping elements. However, if the page contains an element that overlaps in the background, and that I do not want any effect on it when hover (e.g. green square below), the "hoverable" elements of interest (e.g. red circle and blue rectangle below) are dimmed when they are not hover, but when the cursor is still on the background shape.
In other words:
. green square: not "hoverable" (should not have any effect when hover)
. red circle: "hoverable" (i.e. dim all other elements when hover, i.e. blue rectangle and green square)
. blue rectangle: "hoverable" (i.e. dim all other elements when hover, i.e. red circle and green square)
How could I skip dimming all elements when hover the green square?
.parent:hover .child {
opacity: 0.2;
transition: all .3s;
}
.parent .child:hover {
opacity: 1;
transition: all .3s;
}
<svg width="250" height="250">
<g class="parent">
<rect x="0" y="0" width="150" height="150" fill="green"/>
<g class="child">
<circle cx="30" cy="45" r="25" fill="red"/>
</g>
<g class="child">
<rect x="60" y="80" width="60" height="30" fill="blue"/>
</g>
</g>
</svg>
Your only hope (I think) is to use pointer-events: none.
But this will avoid any pointer interaction with the element
.parent:hover .child {
opacity: 0.2;
transition: all .3s;
}
.parent .child:hover {
opacity: 1;
transition: all .3s;
}
.nohover {
pointer-events: none;
}
<svg width="250" height="250">
<g class="parent">
<rect class="nohover" x="0" y="0" width="150" height="150" fill="green"/>
<g class="child">
<circle cx="30" cy="45" r="25" fill="red"/>
</g>
<g class="child">
<rect x="60" y="80" width="60" height="30" fill="blue"/>
</g>
</g>
</svg>
The easiest option is to move your filled rectangle outside of the .parent, at which point the existing CSS will work as intended as the .parents opacity is not being applied to the green background.
.parent:hover .child {
opacity: 0.2;
transition: all .3s;
}
.parent .child:hover {
opacity: 1;
transition: all .3s;
}
<svg width="250" height="250">
<rect x="0" y="0" width="150" height="150" fill="green"/>
<g class="parent">
<g class="child">
<circle cx="30" cy="45" r="25" fill="red"/>
</g>
<g class="child">
<rect x="60" y="80" width="60" height="30" fill="blue"/>
</g>
</g>
</svg>
Try like this:
.parent:hover .child {
opacity: 0.2;
transition: all .3s;
}
.parent:hover rect:hover ~.child,
.parent .child:hover {
opacity: 1;
transition: all .3s;
}
<svg width="250" height="250">
<g class="parent">
<rect x="0" y="0" width="150" height="150" fill="green"/>
<g class="child">
<circle cx="30" cy="45" r="25" fill="red"/>
</g>
<g class="child">
<rect x="60" y="80" width="60" height="30" fill="blue"/>
</g>
</g>
</svg>
As the rect element is before the .child elements, it is possible to check for its :hover in addition to .parent:hover and then style the .child elements accordingly.
Related
I'm tring to have an effect on a picture in my website, the effect is "Image revealing from text on hover
" from this website.
I made a CSS file with this code:
body, html {
height: 100%;
margin: 0;
padding: 0;
background: #E3DFD2
}
effecrforthelogo {
background: url('http://www.uploads.co.il/uploads/images/607163485.png');
background-size: cover;
width: 40vmin;
height: auto;
display: block;
margin: 30vmin auto;
}
text {
font-size: 10px;
transition: font-size .4s ease-out;
font-weight: 900;
font-family: arial;
}
effectforthelogo:hover text {
transition: font-size .4s ease-in;
font-size: 300px;
}
and in my aspx file of the page in my wesite I wrote this HTML code:
<efectforthelogo viewbox="0 0 50 50" width="50" height="50">
<defs>
<mask id="mask" x="0" y="0" width="100" height="49">
<rect x="0.5" y="0.5" width="49" height="49" fill="#fff"/>
<text x="20" text-anchor="middle" y="50" dy="0">S</text>
<text x="25" id="ltrV" text-anchor="middle" y="50" dy="0">V</text>
<text x="30" text-anchor="middle" y="50" dy="0">G</text>
</mask>
</defs>
<rect x="0.5" y="0.5" width="49" height="49" mask="url(#mask)" fill-opacity="1" fill="#E3DFD2"/>
</efectforthelogo>
All it shows is "SVG" on the top of the page, without the picture/the effect.
Please help!
I wanted to see the picture woth the effect, but it only showd the text.
I have a an SVG of text and a mask (animating the mask later on) that I'd initially like to have a fill of white. However, when I set the fill to white, the text goes missing as if the color is transparent instead. Here is the relevant code...
svg {
margin: 0;
color: rgba(255, 255, 255, 1);
height: 100vh;
width: 100vw;
position: fixed;
top: 0;
left: 0;
}
rect {
-webkit-mask: url(#mask);
mask: url(#mask);
fill: #f00;
}
defs {
mask {
text {
font-size: 8vw;
/* Using black to illustrate text fill isn't updating */
fill: #000;
}
}
}
#editText {
#media screen and (max-width: 480px) {
transform: translateY(-10%);
}
}
<svg preserveAspectRatio="xMinYMin meet">
<defs>
<mask id="mask" x="0" y="0" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%"></rect>
<text x="50%" y="50vh" text-anchor="middle">
Text line 1
</text>
<text
id="editText"
x="50%"
y="65vh"
text-anchor="middle"
>
Text line 2
</text>
</mask>
</defs>
<rect x="0" y="0" width="100%" height="100%"></rect>
</svg>
Here is how it looks locally...
Here is how it should look (with white text instead of being transparent)
I've tried a combination of strokes, colors and gradients on the text element with no luck. Any ideas on where I'm going wrong?
If I understand correctly, you want to use the mask to cut the rectangle with the shape of the text so that you can see what is below that cutout. If that is the case, the main issue I see is with the css selectors. You have one selector for all rect elements in the page, which includes the element of the mask. I think you need to make sure that you are affecting only the elements you want to affect. For instance, you can use id and class selectors:
svg {
margin: 0;
color: rgba(255, 255, 255, 1);
height: 100vh;
width: 100vw;
position: fixed;
top: 0;
left: 0;
}
#background{
fill: #ff0;
}
#outrect {
-webkit-mask: url(#mask);
mask: url(#mask);
fill: #f00;
}
.masktext{
stroke: #000;
}
#maskrect{
fill: #fff;
}
#editText {
#media screen and (max-width: 480px) {
transform: translateY(-10%);
}
}
<svg preserveAspectRatio="xMinYMin meet">
<defs>
<mask id="mask" x="0" y="0" width="100%" height="100%">
<rect id="maskrect" x="0" y="0" width="100%" height="100%"></rect>
<text class="masktext" x="50%" y="50vh" text-anchor="middle">
Text line 1
</text>
<text
id="editText"
class="masktext"
x="50%"
y="65vh"
text-anchor="middle"
>
Text line 2
</text>
</mask>
</defs>
<rect id="background" x="0" y="0" width="100%" height="100%"></rect>
<rect id="outrect" x="0" y="0" width="100%" height="100%"></rect>
</svg>
I also added a yellow rectangle below to see that transparency. Now you can modulate the transparency by changing the color of .masktext and #maskrect.
What I want to happen is when you roll over the image, the vector shows up but I also want text to show up as well. Right now, the text is there but it's consistently there. I only want it to appear when you hover over the image.
svg{
background:url('https://vignette.wikia.nocookie.net/lunifer-kingdom/images/f/fb/Wolf_howling_at_moon.jpg/revision/latest?cb=20140105045552');
background-size:cover;
width:40vmin; height:auto;
display:block;
}
h5 {
height: auto;
width: 280px;
padding: 10px;
color: white;
margin-top: -105px;
font-size: 15px;
}
text {
font-size:350px;
transition:font-size .4s ease-out;
font-weight:900;
font-family:arial;
}
svg:hover text{
transition:font-size .4s ease-in;
font-size: 10px;
}
<svg viewbox="0 0 50 50" width="50" height="50">
<defs>
<mask id="mask" x="0" y="0" width="100" height="49">
<rect x="0.5" y="0.5" width="49" height="49" fill="#fff"/>
<text x="18" text-anchor="middle" y="50" dy="0">D</text>
<text x="23" id="ltrV" text-anchor="middle" y="50" dy="0">A</text>
<text x="28" text-anchor="middle" y="50" dy="0">R</text>
<text x="33" text-anchor="middle" y="50" dy="0">K</text>
</mask>
</defs>
<rect x="0.5" y="0.5" width="49" height="49" mask="url(#mask)" fill-opacity="1" fill="#C0C0C0"/>
<div id="overlay">
<h5><center>text</center></h5>
</svg>
Here is the fiddle to solve your problem.
Solution fiddle link
Further I'd like to inform you that, in your layout you have put HTML div tag inside SVG tag, which is not a good approach. Even in must have case, you should add HTML tags inside SVG using foreignObject element. Please refer this link for more info on this :https://bl.ocks.org/Jverma/2385cb7794d18c51e3ab
What I did is put this code outside SVG and gave it opacity 0:
<div id="overlay">
<h5><center>text</center></h5>
</div>
And making opacity 1 on hover using this code:
svg:hover ~ #overlay {
transition: opacity .7s ease-in;
opacity: 1;
}
I have an SVG I made, now it has a couple of blocks that move in a straight line.
sometimes when the blocks move it just adds a stroke above them, although I have stroke disabled.
NOTE: if you look closer you can see the orange strokes above the blocks
here's what it looks like.
#keyframes move {
0% {
transform: translate3d(250px , 0px , 0px);
}
50% {
transform: translate3d(-200px , 0px , 0px);
}
100% {
transform: translate3d(250px , 0px , 0px);
}
}
#loading_bar .cls-2 {
stroke: none;
transition: transform 2s ease-in-out;
animation: move 4s infinite forwards;
}
<svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1080">
<defs>
<style>
.cls-1{fill:#64b2c1;}
.cls-2{fill:orange;}
.cls-3{fill:transparent;stroke-miterlimit:10;}
.cls-4{fill:#191919;}
</style>
</defs>
<g id="background">
<rect class="cls-1" width="1920" height="1080"/>
<rect class="cls-2" x="871" y="769" width="90" height="84"/>
</g>
<g id="loading_bar">
<rect class="cls-4" x="545" y="769" width="792" height="84"/>
<rect class="cls-2" x="789" y="789" width="40" height="40"/>
<rect class="cls-2" x="988" y="789" width="40" height="40"/>
<rect class="cls-2" x="888" y="789" width="40" height="40"/></g></svg>
I have drawn a circle using svg. This circle has a hover effect. I would like to add a link within in the circle and for the link text to change color along with the hover effect.
svg#circle {
height: 250px;
width: 250px;
}
circle {
stroke-dasharray: 700;
stroke-dashoffset: 700;
stroke-linecap: butt;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
-ms-transition: all 2s ease-out;
-o-transition: all 2s ease-out;
transition: all 2s ease-out;
}
circle:hover {
fill: pink;
stroke-dashoffset: 0;
stroke-dasharray: 700;
stroke-width: 10;
}
<svg id="circle">
<circle cx="125" cy="125" r="100" stroke="darkblue" stroke-width="3" fill="green" />
</svg>
You need to add a text element wrapped in an anchor link.
Note, the text element, being on top of the circle will block the hover action on that circle. So, I've wrapped the whole thing in a g group and placed the hover capture on that instead.
svg#circle {
height: 250px;
width: 250px;
}
g circle {
stroke-dasharray: 700;
stroke-dashoffset: 700;
stroke-linecap: butt;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
-ms-transition: all 2s ease-out;
-o-transition: all 2s ease-out;
transition: all 2s ease-out;
}
g:hover circle {
fill: pink;
stroke-dashoffset: 0;
stroke-dasharray: 700;
stroke-width: 10;
}
text {
fill: pink;
font-size: 24px;
}
a:hover text {
fill: blue;
}
<svg id="circle">
<g>
<circle cx="125" cy="125" r="100" stroke="darkblue" stroke-width="3" fill="green" />
<a xlink:href="https://www.google.co.uk/" target="_top">
<text x="50%" y="50%" style="text-anchor: middle">google</text>
</a>
</g>
</svg>
I think this will work :
<svg id="circle">
<a xlink:href="https://www.google.com" style="cursor: pointer" target="_blank">
<circle cx="125" cy="70" r="60" stroke="darkblue" stroke-width="3" fill="green" />
</a>
</svg>
EDIT: Dynamically adding link to SVG Circle.
function addAnchor(){
var dummyElement = document.createElement("div");
dummyElement.innerHTML = '<a xlink:href="https://www.google.com" style="cursor: pointer" target="_blank"></a>';
var htmlAnchorElement = dummyElement.querySelector("a");
var circleSVG = document.getElementById("circle");
htmlAnchorElement.innerHTML = circleSVG.innerHTML;
circleSVG.innerHTML = dummyElement.innerHTML;
}
<svg id="circle">
<circle cx="125" cy="70" r="60" stroke="darkblue" stroke-width="3" fill="green" />
</svg>
<button onclick="addAnchor()">Add Anchor</button>
very simple!..
just wrap the entire SVG in a link...this worked for me anyway!!
initialise the link,
insert svg,
close svg,
close link
<svg style="align-self: center" height="125" width="190" >
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="3%" style="stop-color:rgb(255,255,0);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
<text fill="#000066" font-size="40" font-family="Verdana" x="50" y="86">MBS</text>
Sorry, your browser does not support SVG.
</svg> </a>
Very, Very Simple. Add onClick in tag element,
Using the inLine event handler.
Event handlers are not only for HTML objects, like DIV, TR, P, TABLE, etc.
https://www.w3schools.com/jsref/event_onclick.asp
<svg>
<circle onClick="location.href='https://stackoverflow.com'" cx="50" cy="50" r="25" stroke="darkblue" stroke-width="3" fill="green" />
</svg>