How to manage Z-index with 4 layers in React? - reactjs

How would one go for setting up the z-index, so that the 4 layers are as follow:
background color
logoImg (png with opacity 00.7) (!)
circleImg (png)
coundownClock (text)
I tried changing z-index in every possible way, and I can not get that the 3. circleImg & 4. coundownClock overlap.
#1. background color & #2. logoImg are set correctly as background
TimerScreen.js:
import React from 'react';
import UIfx from 'uifx';
import Logo from "../images/logo.png";
import Circle from "../images/circle.png";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
class TimerScreen extends React.Component {
state = {
minutes: 3,
seconds: 0,
reps: 3
}
render() {
const { minutes, seconds, reps } = this.state
return (
<div className="mainDiv" >
<img
className="logoImg"
style={{
width: "100%",
opacity: "0.07",
position: "absolute",
zIndex: "-1"
}}
src={Logo}
alt="berolina-stralau logo"
/>
<div className="coundownClock"> { minutes < 10 ? `0${ minutes }` : minutes }:{ seconds < 10 ? `0${ seconds }` : seconds } </div>
<img id="circleImg" src={Circle} />
<div className="repetitionsCount"> <i>reps left: {reps}</i> </div>
<div id="buttonDiv">
<Link to="/TimerScreen"
className="goButton">PAUSE</Link>
</div>
<br />
</div>
);
}
}
export default TimerScreen;
App.css part:
circleImg {
width: 100%;
margin-bottom: 40px;
position: "absolute";
z-index: "0";
}
.coundownClock {
display: flex;
justify-content: center;
font-size: 111px;
font-weight: bold;
position: "relative";
z-index: "1";
}
Full code on github.com FilipZafran/Interval-Timer

Definition from https://www.w3schools.com/cssref/pr_pos_z-index.asp:
The z-index property specifies the stack order of an element.
An element with greater stack order is always in front of an element
with a lower stack order.
Note: z-index only works on positioned elements (position: absolute,
position: relative, position: fixed, or position: sticky).
So make sure your elements have a position and z-index to create the wanted order. If I understood you correctly that would be:
background color -> z-index: 0
logoImg (png with opacity 00.7) (!) -> z-index: 1
circleImg (png) -> z-index: 2
coundownClock (text) -> z-index: 3
background color is then in the back and coundownClock in the front
As an advice: It would be easier to wrap the elements you want to stack above each other in a which has position: relative. Inside you put all the elements you want to stack with position: absolute and the z-index for creating your order

Related

How to loop a Draggable slider in React

I have a draggable horizontal slider in my current project and I would like to setting up it also to loop continuously. By loop continuously I mean it should respond to the process of showing images one after another when dragging? Right now, I do have only 3 images in my slider and when I drag slider to the left, slider with its 3rd image and a blank white space starts showing just after. Here at this point I want images to get start again continuously from the very beginning i.e. from the 1st image with aim to cover the white blank space.
Apart, one error I'm getting with my existing code is that when I start to drag slider to right side, suddenly a scroll comes up on browser and keep going in never ending state. By never ending state, I mean it still remain on screen when I drag all my 3 images fully in right direction.
So these are the two things I want to apply and want to resolve in my current project. I'm sharing my code below.
src > Routes > Home > Components > Carousel > Components > SliderDataItems > index.js
import React, { useRef, useEffect } from "react";
import { gsap } from "gsap";
import { Draggable } from "gsap/Draggable";
import { ZoomInOutlined } from '#ant-design/icons'
import { Images } from '../../../../../../Shared/Assets';
import ImagesIcon from '../../../../../../Components/Cells/ImagesIcon'
gsap.registerPlugin(Draggable);
const pictures = [
{
img: Images.xgallery1,
icon: <ZoomInOutlined />
},
{
img: Images.xgallery2,
icon: <ZoomInOutlined />
},
{
img: Images.xgallery4,
icon: <ZoomInOutlined />
},
];
const Slide = ({ img, icon }) => {
return (
<div className="slide">
<div className="image">
<ImagesIcon src={img} />
<div className="icon">
{icon}
</div>
</div>
</div>
);
};
export const Slider = () => {
const sliderRef = useRef(null);
useEffect(() => {
Draggable.create(sliderRef.current, {
type: "x"
});
}, []);
return (
<div className="slider" ref={sliderRef}>
{pictures.map((item, index) => {
return (
<Slide key={index} img={item.img} icon={item.icon} />
);
})}
</div>
);
};
export default Slider;
src > Routes > Home > Components > Carousel > style.scss
.slider {
display: flex;
cursor: unset !important;
overflow: hidden !important;
.slide {
.image {
position: relative;
img {
width: 100% !important;
height: auto !important;
object-fit: cover;
}
.icon {
transition: 0.5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
span {
svg {
font-size: 30px;
color: #fff;
}
}
}
}
}
.image:hover .icon {
opacity: 1;
}
}
.image:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background: rgba(211, 208, 208, 0.6);
opacity: 0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.image:hover:after {
opacity: 1;
}
Here's the link of demo (kindly see just above the footer section) for your reference.
Thank you for any help.
For draggle Slider there is a very lightweight JS Carousel package - siema
It is a great, lightweight carousel that is made with JS. There are also other packages built on top of this purely made for React.
In your case, I would offer to try out react-siema.
With it, you can simply use the carousel like that and it will be draggable by default. Plus, no need to load any css.

How to stop the jumping jack of text change in React

How to smoothly transition from text to svg in react.
As soon as I click on next button shown in pic as ">" ,the text changes to svg.But there is some sort of jumping due to change in overall height.
How can I smoothly transition between two.
Note: Unfortunately, I can't share the code as this is not a personal
project.
You can set the height of the containing element to the heighest element. Therefore you need to place them in a row with a negative margin-right so that they overlay each other. Only the element that you make visible will be shown when you hide the other elements with visibility: hidden;
Here is an example:
var elements = document.querySelectorAll('.block__element');
document.querySelector('button').addEventListener('click', () => {
elements.forEach(element => {
if(element.classList.contains('block__element--visible')) {
element.classList.remove('block__element--visible');
} else {
element.classList.add('block__element--visible');
}
})
});
.block {
background: #aaa;
display: flex;
}
.block__element {
width: 100%;
margin-right: -100%;
visibility: hidden;
opacity: 0;
transition: visibility 1s, opacity 1s;
}
.block__element--visible {
visibility: visible;
opacity: 1;
}
<div class="block">
<div class="block__element block__element--visible">
<div style="background: #f00">Content 1</div>
</div>
<div class="block__element">
<div style="height: 200px; background: #0f0">Longer content 2</div>
</div>
</div>
<button>Toggle between elements</button>

Build version of scrollmagic and gsap in react not the same as dev version

I'm trying to use ScrollMagic & gsap libraries in react, everything was fine until I tried to build my code, I tried all the import solutions I could find on the web but it doesn't seem to behave the same way.
I created a list of cards that scrolls horizontally when some section is reached, on the dev version it behaves exactly that way but when I'm in a production version (after build) it scrolls in diagonal way.
Here's some of my code and how I installed the libraries:
installing gsap:
npm i gsap
installing scrollmagic:
npm i scrollmagic
my code in react:
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
import { TweenMax, TimelineLite, TimelineMax, TweenLite, Linear } from "gsap/all"; // Also works with TweenLite and TimelineLite
import * as ScrollMagic from "scrollmagic/scrollmagic/uncompressed/ScrollMagic"; // Or use scrollmagic-with-ssr to avoid server rendering problems
import { ScrollMagicPluginGsap } from "scrollmagic-plugin-gsap";
import './listCardsH.css';
import Image from '../images/Image';
import Titles from '../titles/Titles';
ScrollMagicPluginGsap(ScrollMagic, TweenMax, TimelineLite);
class ListCardsH extends Component {
componentDidMount (){
TweenLite.defaultEase = Linear.easeNone;
var titles = ReactDOM
.findDOMNode(this.refs['title1'])
.getBoundingClientRect()
console.log(titles)
var controller = new ScrollMagic.Controller();
// var tl = new TimelineLite();
// tl.to("#js-slideContainer", 1, {x:'-100%'})
var tl = new TimelineMax()
.add(TweenMax.to('#js-slideContainer', 1, {x: '-70%'}))
new ScrollMagic.Scene({
triggerElement: '#bigTrigger',
triggerHook: 0,
duration: '100%'
})
.setPin("#titleId", {pushFollowers: false})
.addTo(controller);
new ScrollMagic.Scene({
triggerElement: "#bigTrigger",
triggerHook: 0 ,
duration: "100%"
})
.setPin("#js-slideContainer")
.setTween(tl)
.addTo(controller);
}
render() {
return (
<div className="containerWrapper" id="bigTrigger">
<div className='bigTitle' id='titleId'>
<Titles></Titles>
</div>
<div className="wrapper" id="js-wrapper" >
<div className="sections " id="js-slideContainer">
<div className='childCardH section' ref='title1'>
<div className='sectionTitle'>
<Image srcImg='https://www.inform.kz/radmin/news/2019/11/03/191103143718887e.jpg'></Image>
</div>
</div>
<div className='childCardH section' ref='title1'>
<div className='sectionTitle'>
<Image srcImg='https://www.inform.kz/radmin/news/2019/11/03/191103143718887e.jpg'></Image>
</div>
</div>
<div className='childCardH section' ref='title1'>
<div className='sectionTitle'>
<Image srcImg='https://www.inform.kz/radmin/news/2019/11/03/191103143718887e.jpg'></Image>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default ListCardsH;
CSS
.wrapper {
width: 130%;
height: 100%;
perspective: 1000;
-webkit-perspective: 1000;
perspective-origin: 0;
-webkit-perspective-origin: 0;
overflow: hidden;
}
.section {
height: 120%;
width: calc( 100% / 3);
float: left;
position: relative;
}
.sections {
width: 100%;
height: 50vh;
/* Change this height to move the images up and down */
}
.sectionTitle {
position: absolute;
width: 40vw;
top: 50%;
left: 120%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
font-size: 30px;
color: #fff;
}
.bigTitle {
width: 100vw;
text-align:center;
padding-right: 10vw;
}
The problem that was causing that unwanted behavior was the perspective element on the css, adding a perspective is necessary when using a 3d animation but in this case it should be not used since the animation is just a 2d scroll, that transforms the verticall scroll to a horizontal one, it took me couple of days to figure this out and I hope it will help others build such nice animations. There were some other issues with my code but I fixed everything and wanted to share this so it can be a reference to anyone who would want to make something similar.
Please find down below the final version of my code with a live demo on CodePen:
Live Demo Of 'React + ScrollMagic + Gsap : Horizontal with pined title to top' created by me.

How to do top image transition to show the background image while scrolling

I'm doing SPA wherein I've sticky header and a half background image with 3 cards (having a title and image) on, the only thing I'm being stuck is when, I scroll down the top image must have transitioned. when the page is getting scrolled the background image should appear in that much of image transition. when I scroll back up the image must do transition expanding itself and hiding the background image.
post.scss:
#import '../scss/common.scss';
.container{
background-image: url("../images/bg2.jpg") ;
height:80%;
//background-position: center;
background-repeat: no-repeat;
background-size: 100%;
}
.header{
// background-color: $primary-background;
padding: 10px 16px;
// color: #f1f1f1;
height: 50px;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.sticky + .content {
padding-top: 100px;
}
.blank{
height: 800px;
}
post.js:
import React from 'react';
import TitleContainer from '../container/titleContainer';
import './post.scss';
import Cards from '../container/cards';
import imgT from '../images/city1.jpg';
class Post extends React.Component{
constructor(props) {
super(props);
this.state={
}
this.header = React.createRef();
}
handscroll=(e)=>{
const node = this.header.current;
let sticky=node.offsetTop
//console.log(node)
// console.log(sticky)
if (window.pageYOffset > sticky) {
node.classList.add("sticky");
} else {
node.classList.remove("sticky");
}
// console.log(e)
}
componentDidMount(){
window.addEventListener('scroll',this.handscroll)
}
render(){
return(
<div className="container" >
<div className="header" ref={this.header}>
<TitleContainer title="World Top Cities"/>
</div>
<div className="content">
<Cards title="t1" cntryImg={imgT} />
<Cards title="t2" cntryImg={imgT} />
<Cards title="t3" cntryImg={imgT} />
<div className="blank"></div>
</div>
</div>
)
}
}
export default Post;
card.scss:
%common-all{
width: 100%;
}
//border:1px solid #80808040;
//height:250px;
// border-radius: 0 0 15px 15px ;
#mixin commonFn($border,$height,$borderRadius){
border:$border;
height:$height;
border-radius: $borderRadius ;
}
.container3{
#extend %common-all;
// border-radius: 40px;
margin-top:3%;
.title{
#extend %common-all;
#include commonFn(1px solid #80808040,50px,15px 15px 0 0);
background-color: white;
}
.cntryImg{
#extend %common-all;
#include commonFn(0,250px,0 0 15px 15px)
}
}
cards.js:
import React from 'react';
import '../scss/cards.scss'
class Cards extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<div className="container3">
<div className="title">
{this.props.title}
</div>
< div className="cntryImg1">
<img src={this.props.cntryImg} alt="noImg" className="cntryImg"/>
</div>
</div>
)
}
}
export default Cards;
I don't know where I'm going wrong, I do have any much no idea how do I approach it. when the page is getting scrolled down the background image should appear in that much of image transition (here image transists). When I scroll back up the image must do transition expanding itself and hiding the background image.
Please help I'm stuck past two days. Let me know if I'm not clear. Please help me on how doing it.
Updated
No, not height theirs half background image, out of 3 "containers3"(cards) 2 "containers3"(cards) covering the background image one is white background when I scroll down there should be transition effect the suppose 10px (just for ex) I scroll only that many pixels transition must happen for that particular card (not hidden at once transition effect way of hiding and showing background image when scroll up or down respectively) and background image for that particular 10px must be shown, position must not change. (here theirs transition way of hiding and showing background image)

How do I use flex direction in ordered list in NavBar with Reactjs?

Hi I am building a NavBar in ReactJs without any UI library like bootstrap or material just CSS and I want to use flex-direction: row in ol but its not working.
You can see my code below for NavBar component.
import React, { Component } from 'react';
import './landingnav.css';
export default class LandingNav extends Component {
render() {
return (
<div className="navcontainer">
<div className="navbarlogo">
<h3>Codolas</h3>
</div>
<div className="navitems">
<ol className="leftlist">
<li>Why Codolas?</li>
<li>Solutions</li>
<li>About us</li>
</ol>
</div>
</div>
);
}
}
Here is CSS for the same component
.navcontainer {
background-color: rgb(97, 252, 162);
height: 84px;
top: 0;
width: 100%;
margin-top: -18px;
}
.navbarlogo h3 {
position: absolute;
font-size: 28px;
color: rgb(0, 0, 0);
left: 0px;
}
.leftlist {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
li {
list-style-type: none;
}
Try giving the list items a flex: 1, the children don't know how much to flex across the space.
I just found the issue actually after adding position: relative to navcontainer, I just simply added ol and put the styles of flex in it and it worked. Thanks to all. Happy coding

Resources