英文:
Nextjs render a new div onClick
问题
以下是您提供的内容的中文翻译:
- 单击时渲染 div
 
function zoom_img(event) {
    console.log(event.target);
    console.log(event.target['data-loaded-src'])
    const src = event.target['data-loaded-src']
    
    return (
      <div className='w-screen h-screen fixed flex justify-center items-center'>
        <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
          <Image src={src} fill={true} />
        </div>
      </div>
    )
  }
...
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={zoom_img}/>
- 自定义组件
 
function Glasses ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      return (
        <div className='w-screen h-screen fixed flex justify-center items-center'>
          <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
            <Image src={src} fill={true} />
          </div>
        </div>
      )
    }
    return (
      <Image src={src} width={width} height={height} alt={alt} onClick={enlrg} />
    )
  }
...
<Glasses src={'/gafas.png'} width={150} height={150} alt='Gafas 1' />
- 单击现有的 div 以移除隐藏类
 
function Glasses2 ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      let x = document.getElementById('temp');
      x.classList.remove('hidden');
      x.classList.add('flex');
    }
    return (
      <Image src={src} width={width} height={height} alt={alt} onClick={enlrg} />
    )
  }
...
<Glasses2 src={'/gafas.png'} width={150} height={150} alt='Gafas 1' />
<div id='temp' className='w-screen h-screen fixed hidden justify-center items-center'>
  <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
    <Image src={'/gafas.png'} fill={true} />
  </div>
</div>
- 这个方法确实可行,但我失去了 Next.js 图像优化
 
function zoom_img (event) {
    console.log(event.target);
    console.log(event.target['data-loaded-src'])
    const src = event.target['data-loaded-src']
    const new_cont = document.createElement('div');
    const new_div = document.createElement('div');
    const main = document.getElementById('main');
    new_cont.classList.add('w-screen', 'h-screen', 'fixed', 'flex', 'justify-center', 'items-center');
    new_div.classList.add('w-9/12', 'h-5/6', 'bg-slate-200', 'opacity-50');
    // 在这里添加 img 标签
    new_cont.appendChild(new_div);
    main.appendChild(new_cont);
  }
...
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={zoom_img} />
在每种方法中,您都可以在控制台中看到日志,但只有第四种方法中会看到新的 div。是否第四种方法是唯一正确的方式?如果可能的话,我更喜欢使用 Image 组件。您认为哪种方法更好?
英文:
I have an image and I want to create a new div (on click) with a larger version of the same image.
I've tried four approaches:
- Render div on Click
 
function zoom_img (event) {
    console.log(event.target);
    console.log(event.target['data-loaded-src'])
    const src = event.target['data-loaded-src']
    
    return (
      <div className='w-screen h-screen fixed flex justify-center items-center'>
        <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
          <Image src={src} fill={true} />
        </div>
      </div>
    )
  }
...
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={zoom_img}/>
- Custom component
 
function Glasses ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      return (
        <div className='w-screen h-screen fixed flex justify-center items-center'>
          <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
            <Image src={src} fill={true} />
          </div>
        </div>
      )
    }
    return (
      <Image src={src} width={width} height={height} alt={alt} onClick={enlrg} />
    )
  }
...
<Glasses src={'/gafas.png'} width={150} height={150} alt='Gafas 1' />
- Existing div onClick remove hidden class
 
function Glasses2 ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      let x = document.getElementById('temp');
      x.classList.remove('hidden');
      x.classList.add('flex');
    }
    return (
      <Image src={src} width={width} height={height} alt={alt} onClick={enlrg} />
    )
  }
...
<Glasses2 src={'/gafas.png'} width={150} height={150} alt='Gafas 1' />
<div id='temp' className='w-screen h-screen fixed hidden justify-center items-center'>
  <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
    <Image src={'/gafas.png'} fill={true} />
  </div>
</div>
- This one DID work but I'm loosing nextjs image optimization
 
function zoom_img (event) {
    console.log(event.target);
    console.log(event.target['data-loaded-src'])
    const src = event.target['data-loaded-src']
    const new_cont = document.createElement('div');
    const new_div = document.createElement('div');
    const main = document.getElementById('main');
    new_cont.classList.add('w-screen', 'h-screen', 'fixed', 'flex', 'justify-center', 'items-center');
    new_div.classList.add('w-9/12', 'h-5/6', 'bg-slate-200', 'opacity-50');
    // add img tag here
    new_cont.appendChild(new_div);
    main.appendChild(new_cont);
  }
...
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={zoom_img} />
In every approach I DO get to see the log in the console but only in approach 4 I see the new div.
Is approach 4 the only correct way? I would prefer to use the Image component if possible. What do you guys think?
答案1
得分: 1
你可以尝试类似这样的方式:
设置钩子:
[zoomImage, setZoomImage] = useState(false)
<Image src={'/gafas.png'} width={150} height={150} alt={'Gafas 1'} onClick={() => setZoomImage(true)}/>
// 放大后的图片
<div className={`${zoomImage ? 'block' : 'hidden'} w-screen h-screen fixed flex justify-center items-center`}>
<div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
   <Image src={src} fill={true} />
</div>
</div>
英文:
You can try something like this:
Set the hook:
[zoomImage, setZoomImage] = useState(false)
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={() => setZoomImage(true)}/>
// zoomed image
<div className={`${zoomImage ? 'block' : 'hidden'} w-screen h-screen fixed flex justify-center items-center`}>
<div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
   <Image src={src} fill={true} />
</div>
</div>
In this code you are actually telling Next.JS that whenever user clicks that image, change variable zoomImage to true and as a result that bigger image that is hidden by default, will have display block. (I supposed you're using tailwindcss)
答案2
得分: 0
谢谢 @Atena Dadkhah。你的答案完美地解决了问题。
在项目中,我不只有一个图像,而是有一堆图像,所以最终的代码看起来像这样:
function enlarge(e) {
    setLrg(true);
    let img = document.getElementById('largerImage'),
        src = e.target['data-loaded-src'];
    img['src'] = src;
}
然后是一堆类似这样的图像:
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={enlarge} className='cursor-zoom-in' />
接着是隐藏的 div:
<div className={`${lrg ? 'flex' : 'hidden'} w-screen h-screen fixed justify-center items-center`}>
  <div className='w-9/12 h-5/6 bg-slate-200 flex justify-center items-center relative'>
    <Image id='largerImage' fill={true} />
  </div>
</div>
仍然缺少关闭 div 的行为,但应该很容易添加。
再次感谢 ![]()
英文:
Thank you very much @Atena Dadkhah. Your answer worked perfectly.
In the project a have not one but a bunch of images so the final code looked like this:
function enlarge(e) {
    setLrg(true);
    let img = document.getElementById('largerImage'),
        src = e.target['data-loaded-src'];
    img['src'] = src;
  }
Then a bunch of images like this:
<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={enlarge} className='cursor-zoom-in' />
Then the hidden div:
<div className={`${lrg ? 'flex' : 'hidden'} w-screen h-screen fixed justify-center items-center`}>
  <div className='w-9/12 h-5/6 bg-slate-200 flex justify-center items-center relative'>
    <Image id='largerImage' fill={true} />
  </div>
</div>
It's still missing the behaviour for closing the div but that should be easy to add.
Once again. Thank you ![]()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论