Magazine Cover Slider using ReactJs
Magazine Cover Slider using ReactJs
Magazine Cover Slider using ReactJs
Html
<link href=”https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css” rel=”stylesheet” integrity=”sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN” crossorigin=”anonymous”> <div id=”root”></div>
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
@import url(https://fonts.googleapis.com/css?family=Josefin+Slab:100); :root { --slide-width: 300px; --slide-height: 500px; } #root { position: fixed; } .slider-wrap { width: var(--slide-width); height: var(--slide-height); position: fixed; left: 10%; top: 10%; } .slider { overflow: hidden; position: relative; font-family: "Segoe UI", "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif; } .holder { display: flex; position: relative; } .shift { transform: translateX(0%); transition: transform 300ms cubic-bezier(0.455, 0.03, 0.515, 0.955); } .resetLeft { transform: translateX(-100%); transition: none; } .slide { flex: 1; min-width: var(--slide-width); width: var(--slide-width); height: var(--slide-height); position: relative; background-position: center; background-repeat: no-repeat; background-size: cover; } .slide:before { content: ""; position: absolute; bottom: 0; width: 100%; height: 110%; background: linear-gradient(black, transparent, transparent, black); } .navSlider { height: 100%; position: absolute; top: 0px; left: -10%; display: flex; width: 120%; align-items: center; justify-content: space-between; } .navSlider line { stroke: #ffffffcc; stroke-width: 10; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 83; stroke-dashoffset: -83; transition: stroke-dashoffset 300ms; } .navSlider svg { overflow: visible; cursor: pointer; } #rightArrow { -webkit-filter: drop-shadow(9px 5px 6px rgba(16,17,21,.8)); filter: drop-shadow(9px 5px 6px rgba(16,17,21,.8)); } #leftArrow { -webkit-filter: drop-shadow(-9px 5px 6px rgba(16,17,21,.8)); filter: drop-shadow(-9px 5px 6px rgba(16,17,21,.8)); } .navSlider:hover line { stroke-dashoffset: 0; transition: stroke-dashoffset 400ms; } .navSlider svg:hover line { stroke: white; } .text { position: absolute; color: white; top: 15px; left: 15px; font-size: 30px; font-weight: 700; opacity: 0; transform: translateX(100%); } .animateSlader .central .text { display: block; opacity: 1; transform: translateX(0%); transition: transform 300ms cubic-bezier(0.455, 0.03, 0.515, 0.955); } .top { bottom: 13px; font-size: 23px; right: 10px; opacity: 0; position: absolute; color: white; transform: translateX(-100%); } .animateSlader .central .top { display: block; opacity: 1; transform: translateX(0%); transition: transform 400ms, opacity 200ms cubic-bezier(0.455, 0.03, 0.515, 0.955); } .reviewPic { position: absolute; bottom: 10px; left: 10px; width: 36px; height: 36px; border-radius: 50%; border: 2px solid white; } .reviewerName { bottom: 28px; font-size: 14px; left: 58px; position: absolute; color: white; opacity: 0; transform: translateY(-100%); } .animateSlader .central .reviewerName { display: block; opacity: 1; transform: translateY(0%); transition: transform 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955); } .reviewerPosition { bottom: 11px; font-size: 14px; left: 57px; position: absolute; color: white; opacity: 0; transform: translateY(100%); } .animateSlader .central .reviewerPosition { display: block; opacity: 1; transform: translateY(0%); transition: transform 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955); } .review { bottom: 44px; font-size: 13px; position: absolute; color: white; line-height: 1.3; padding: 15px; } |
Js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
const data = { cards: [ { index: 0, picture: "https://cdn.pixabay.com/photo/2018/01/11/09/52/two-3075753_960_720.jpg", description: "#fashion", reviewPic: "https://cdn.pixabay.com/photo/2017/07/31/21/01/laptop-2561018_1280.jpg", reviewerName: "Olivia Bell", reviewerPosition: "Freelancer", review: '"Lorem ipsum dolor sit amet, ea est patrioque cotidieque, accusam patrioque consequuntur vim at. Rebum nulla melius cu usu, pro erant abhorreant moderatius ei. Errem oporteat gloriatur ad est."', }, { index: 1, picture: "https://cdn.pixabay.com/photo/2016/01/19/19/26/amsterdam-1150319_1280.jpg", description: "#tonight", reviewPic: "https://pixabay.com/images/download/woman-6212065_640.jpg", reviewerName: "Briam Cute", reviewerPosition: "Beauty/Fashion", review: '"Lorem ipsum dolor sit amet, ea est patrioque cotidieque, accusam patrioque consequuntur vim at. Rebum nulla melius cu usu"', }, { index: 2, picture: "https://cdn.pixabay.com/photo/2019/03/10/18/31/hong-kong-4046913_1280.jpg", description: "#hongkong", reviewPic: "https://images.pexels.com/photos/936229/pexels-photo-936229.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260", reviewerName: "William White", reviewerPosition: "Project Manager", review: '"Lorem ipsum dolor sit amet, ea est patrioque cotidieque, accusam patrioque consequuntur vim at. Rebum nulla melius cu usu, pro erant abhorreant moderatius ei. Errem oporteat gloriatur ad est."', }, { index: 3, picture: "https://cdn.pixabay.com/photo/2019/10/31/06/58/avenue-4591121_1280.jpg", description: "#fall", reviewPic: "https://pixabay.com/images/download/woman-1439909_640.jpg", reviewerName: "Emma Doran", reviewerPosition: "FamilyPhotoStudio", review: '"Lorem ipsum dolor sit amet, ea est patrioque cotidieque, accusam patrioque consequuntur vim at. Rebum nulla melius cu usu, pro erant abhorreant moderatius ei. Errem oporteat gloriatur ad est."', }, { index: 4, picture: "https://cdn.pixabay.com/photo/2019/11/19/02/05/seascape-4636264_1280.jpg", description: "#tide", reviewPic: "https://images.pexels.com/photos/428328/pexels-photo-428328.jpeg", reviewerName: "Lorenzo Papapietro", reviewerPosition: "Art Director", review: '"Lorem ipsum dolor sit amet, ea est patrioque cotidieque, accusam patrioque consequuntur vim at. Rebum nulla melius cu usu, pro erant abhorreant moderatius ei. Errem oporteat gloriatur ad est."', }, { index: 5, picture: "https://cdn.pixabay.com/photo/2019/05/23/02/15/marching-band-4223023_1280.jpg", description: "#parade", reviewPic: "https://images.pexels.com/photos/935969/pexels-photo-935969.jpeg", reviewerName: "Samuel Karter", reviewerPosition: "Consulting", review: '"Lorem ipsum dolor sit amet, ea est patrioque cotidieque, accusam patrioque consequuntur vim at. Rebum nulla melius cu usu, pro erant abhorreant moderatius ei. Errem oporteat gloriatur ad est."', }, { index: 6, picture: "https://cdn.pixabay.com/photo/2017/08/10/04/29/girl-2618180_1280.jpg", description: "#bicycle", reviewPic: "https://images.pexels.com/photos/555790/pexels-photo-555790.png", reviewerName: "Elliott Finn", reviewerPosition: 'CEO "Cordex"', review: '"Accusam patrioque consequuntur vim at. Rebum nulla melius cu usu, pro erant abhorreant moderatius ei. Errem oporteat gloriatur ad est."', }, ], }; class MyApp extends React.Component { constructor(props) { super(props); this.state = { index: 0, translate: 0, shift: "", posInitial: "posInitial", posFinal: "12", posX1: "", transit: -100, posX2: "", leftSlide: data.cards.length - 1, centralSlide: 0, rightSlide: 1, class: "animateSlader", navSlider: "", }; this.click = this.click.bind(this); this.shifting = this.shifting.bind(this); this.loading = this.loading.bind(this); this.dragStart = this.dragStart.bind(this); this.dragMove = this.dragMove.bind(this); this.dragEnd = this.dragEnd.bind(this); this.displayNav = this.displayNav.bind(this); } componentDidMount() { window.addEventListener("mousemove", this.displayNav); } displayNav() { window.removeEventListener("touchstart", this.displayNav); this.setState({ navSlider: ( <nav className="navSlider"> <svg version="1.1" onClick={this.click.bind(this, +1)} id="leftArrow" xmlns="http://www.w3.org/2000/svg" x="0px" preserveAspectRatio="xMaxYMin meet" width="80" height="150" viewBox="0 0 48.2 150" > <line x1="4.4" y1="75" x2="43.8" y2="3" /> <line x2="43.8" y2="147" x1="4.4" y1="75" /> </svg> <svg version="1.1" onClick={this.click.bind(this, -1)} id="rightArrow" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" width="80" height="150" viewBox="0 0 48.2 150" > <line x1="43.8" y1="75" x2="4.4" y2="3" /> <line x2="4.4" y2="147" x1="43.8" y1="75" /> </svg> </nav> ), }); } loading(get) { this.setState({ class: "animateSlader", }); let index = this.state.index - get; let prevSlide = index - 1; let nextSlide = index + 1; switch (index) { case -1: index = data.cards.length - 1; prevSlide = index - 1; nextSlide = 0; break; case data.cards.length - 1: nextSlide = 0; break; case 0: prevSlide = data.cards.length - 1; break; case data.cards.length: index = 0; prevSlide = data.cards.length - 1; nextSlide = index + 1; } this.setState({ leftSlide: prevSlide, centralSlide: index, rightSlide: nextSlide, index: index, transit: -100, }); } shifting(shift) { this.setState({ transit: shift, class: "shift", }); } click(get) { let shift = -100 + get * 100; setTimeout(this.shifting, 0, shift); setTimeout(this.loading, 280, get); } dragStart(e) { e = e || window.event; e.preventDefault(); this.setState({ posX1: e.touches[0].clientX, posInitial: document.getElementById("root").offsetTop, }); } dragMove(e) { e = e || window.event; e.preventDefault(); let move = (e.touches[0].clientX - this.state.posX1) / 3; if (move <= -95 || move >= 95) { return; } else { this.setState({ posX2: move, transit: move - 100, posFinal: e.touches[0].clientY, }); } } dragEnd() { if (this.state.posX2 < 0) { if (this.state.posX2 < -20) { setTimeout(this.shifting, 0, -200); setTimeout(this.loading, 300, -1); } else { this.shifting(-100); } } else if (this.state.posX2 > 0) { if (this.state.posX2 > 20) { this.shifting(0); setTimeout(this.loading, 300, 1); } else { this.shifting(-100); } } } render() { return ( <div className="slider-wrap" id="slider-wrap"> <div className="slider" id="slider"> <div id="holder" style={{ transform: "translateX(" + this.state.transit + "%)" }} className={"holder " + this.state.class} onTouchStart={this.dragStart} onTouchMove={this.dragMove} onTouchEnd={this.dragEnd} > <div className="slide" style={{ backgroundImage: "url(" + data.cards[this.state.leftSlide].picture + ")", }} > <span className="text"> {data.cards[this.state.leftSlide].description} </span> </div> <div className="slide central" style={{ backgroundImage: "url(" + data.cards[this.state.centralSlide].picture + ")", }} > <span className="text"> {data.cards[this.state.centralSlide].description} </span> <img src={data.cards[this.state.centralSlide].reviewPic} alt={data.cards[this.state.centralSlide].reviewerFirstName} className="reviewPic" /> <span className="reviewerName"> {data.cards[this.state.centralSlide].reviewerName} </span> <span className="reviewerPosition"> {data.cards[this.state.centralSlide].reviewerPosition} </span> <span className="review"> {data.cards[this.state.centralSlide].review} </span> <span className="top"> <i className="fa fa-star-o"></i> <i className="fa fa-star-o"></i> <i className="fa fa-star-o"></i> <i className="fa fa-star-o"></i> <i className="fa fa-star-o"></i> </span> </div> <div className="slide" style={{ backgroundImage: "url(" + data.cards[this.state.rightSlide].picture + ")", }} ></div> </div> </div> {this.state.navSlider} </div> ); } } ReactDOM.render(<MyApp />, document.getElementById("root")); |
Source: https://codepen.io/maxxsh/pen/qzMxQX