0
var imgs = [
      'pic1.gif',
      'pic2.jpg',
      'pic3.jpeg',
      'pic4.gif',
      'pic5.jpeg',
      'pic6.jpeg',
      'pic7.jpeg',
      'pic8.jpg',
      'pic9.webp'
    ],
    n = imgs.length,
    current = n-1,
    closedWidth = Math.floor(window.innerWidth/10)



for (var i=0; i<n; i++){
  
  var bgImg = document.createElement('div');
  bg.appendChild(bgImg);
  
  gsap.set(bgImg, {
    attr:{id:'bgImg'+i, class:'bgImg'},
    width:'100%',
    height:'100%',
    backgroundImage:'url('+imgs[i]+')',
    backgroundSize:'cover',
    backgroundPosition:'center'
  })
  
  var b = document.createElement('div');
  fg.appendChild(b);
  
  gsap.fromTo(b, {
    attr:{id:'b'+i, class:'box'},
    innerHTML:'<p><sub>Figtab.</sub> '+(i+1)+'</p>',
    width:'100%',
    height:'100%',
    borderLeft:(i>0)?'solid 1px #eee':'',
    backgroundColor:'rgba(250,250,250,0)',
    left:i*closedWidth,
    transformOrigin:'100% 100%',
    x:'100%'
  },{
    duration:i*0.15,
    x:0,
    ease:'expo.inOut'
  })  
  
  b.onmouseenter = b.onclick = (e)=>{    
    if (Number(e.currentTarget.id.substr(1))==current) return;
     
    var staggerOrder = !!(current < Number(e.currentTarget.id.substr(1)));
    current = Number(e.currentTarget.id.substr(1));
    gsap.to('.box', {
      duration:0.5,
      ease:'elastic.out(0.3)',
      left:(i)=>(i<=current)? i*closedWidth: window.innerWidth-((n-i)*closedWidth),
      x:0,
      stagger: staggerOrder? 0.05:-0.05
    })
    
    bg.appendChild( document.getElementById('bgImg'+current) )
    gsap.fromTo('#bgImg'+current, {opacity:0}, {opacity:1, duration:0.3, ease:'power1.inOut'})
    gsap.fromTo('#bgImg'+current, {scale:1.05, rotation:0.05}, {scale:1, rotation:0, duration:1.5, ease:'sine'}) 
  }
}


window.onresize = (e)=>{
  closedWidth = Math.floor(window.innerWidth/10)
  gsap.set('.box', { x:0, left:(i)=> (i<=current)? i*closedWidth: window.innerWidth-((n-i)*closedWidth) })
}
  • 1
    What do you mean by "captions"? And also what/whose code are you using (a link to the library, plugin)? – mardubbles Apr 19 '22 at 08:43
  • Thank you for answering. I got this from code pen. (https://codepen.io/creativeocean/pen/XWpowEj). I just wanted to put a caption under each image. – Puskar wagle Apr 19 '22 at 08:45
  • It's a cool looking plugin. I'd suggest inspecting the code to see where "they" put the "header" numbers in at. Then from there see if you can add an element below (or absolute positioning) on the image itself, for the captions. Unless "they" already have built in support for captions. – mardubbles Apr 19 '22 at 08:58
  • Again, if you could provide the link (a github?) on where this code is that you are using, I could take a look at it for you. – mardubbles Apr 19 '22 at 09:03
  • I got it from codepen. Here's is the link. https://codepen.io/creativeocean/pen/XWpowEj This is all i know of it. Please take a look at the js. – Puskar wagle Apr 19 '22 at 09:08
  • Ok under the "js" pane in the code pen, make this var (make it "var" so its global scoped, for now): `var captions = ["apple","orange","pear","grape","peach"];` ... Then under the "innerHTML" part you mention, make that read `innerHTML:'

    Fig. '+(captions[i])+'

    '`. .... You'll notice the fruit names per image, iterated on that array.
    – mardubbles Apr 19 '22 at 09:15
  • Glad you got it solved. – mardubbles Apr 19 '22 at 09:25

0 Answers0