简体   繁体   English

Gif 打破了 Gatsby 网站的响应能力

[英]Gif breaking the responsiveness of Gatsby site

  • Problem Summary问题总结

There are two.gif images in my blog post, which are breaking the responsiveness of my site, they don't seem to get resized when opened on a mobile device.我的博客文章中有两个 .gif 图片,它们破坏了我网站的响应能力,在移动设备上打开时它们似乎没有调整大小。 Although they seem to be fine when opened from pc.虽然从电脑打开时它们似乎很好。

PC view:电脑视图:

图片

Mobile view:移动视图:

图片

As you can see, in mobile view the two.gif images are still the same size, which breaks the responsiveness of the page.如您所见,在移动视图中,two.gif 图像的大小仍然相同,这会破坏页面的响应能力。 Is there a way I could solve this issue?有没有办法解决这个问题?



  • The syntax I've used to include the.gif in my.mdx file is-我用来在 my.mdx 文件中包含 the.gif 的语法是 -

    .[otter dancing with a fish](./neural_net_data_manupulation_2.gif)

  • Config.js file of my site:我网站的 Config.js 文件:

require(`dotenv`).config({
  path: `.env`,
})


const shouldAnalyseBundle = process.env.ANALYSE_BUNDLE

module.exports = {
  siteMetadata: {
    siteTitle: `Khalid Saifullah`,
    siteTitleAlt: `Khalid Saifullah - Blog`,
    author: `@k_saifullaah`,
    siteImage: `/my_banner.jpg`,
    siteHeadline: `Khalid Saifullah - Blog`,
    siteUrl: `https://khalidsaifullaah.github.io`,
    siteDescription: `I'm Khalid - a CS undergrad student. I take great interest in Deep Learning research, visualizations, and photography.`,
  },
  plugins: [
    // `gatsby-remark-mathjax`,
    {
      resolve: `@lekoarts/gatsby-theme-minimal-blog`,
      // See the theme's README for all available options
      options: {
        mdx: false,
        feedTitle: `Khalid Saifullah - Blog`,
        navigation: [
          {
            title: `Blog`,
            slug: `/blog`,
          },
          {
            title: `About`,
            slug: `/about`,
          },
          {
            title: `Portfolio`,
            slug: `/portfolio`,
          },
        ],
        externalLinks: [
          {
            name: `Github`,
            url: `https://github.com/khalidsaifullaah`,
          },
          {
            name: `Twitter`,
            url: `https://twitter.com/k_saifullaah`,
          },
          {
            name: `Facebook`,
            url: `https://www.facebook.com/ikhalidsaifullaah/`,
          },
        ],
      },
    },
    {
            resolve: `gatsby-plugin-mdx`,
            options: {
        gatsbyRemarkPlugins: [
          `gatsby-remark-copy-linked-files`, 
          { 
            resolve: `gatsby-remark-images`, 
            options: { 
              linkImagesToOriginal: false, 
            }, 
          },
        ],
                remarkPlugins: [ require('remark-math'), require('remark-html-katex') ]
            }
        },
    
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [`gatsby-remark-copy-linked-files`],
      },
    },
    {
      resolve: `gatsby-plugin-google-analytics`,
      options: {
        trackingId: process.env.GOOGLE_ANALYTICS_ID,
      },
    },
    `gatsby-plugin-sitemap`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `Khalid Saifullah - Blog`,
        short_name: `minimal-blog`,
        description: `I'm Khalid - a CS undergrad student at United International University. I take great interest in Deep Learning research, visualizations, and photography. Another aspect of me is that I'm a cinephile`,
        start_url: `/`,
        background_color: `#fff`,
        theme_color: `#6B46C1`,
        display: `standalone`,
        icons: [
          {
            src: `/android-chrome-192x192.png`,
            sizes: `192x192`,
            type: `image/png`,
          },
          {
            src: `/android-chrome-512x512.png`,
            sizes: `512x512`,
            type: `image/png`,
          },
        ],
      },
    },
    `remark-math`,
    `remark-html-katex`,
    `gatsby-plugin-offline`,
    `gatsby-plugin-netlify`,
    shouldAnalyseBundle && {
      resolve: `gatsby-plugin-webpack-bundle-analyser-v2`,
      options: {
        analyzerMode: `static`,
        reportFilename: `_bundle.html`,
        openAnalyzer: false,
      },
    },
  ].filter(Boolean),
}

You can check out the live version of the post here您可以在此处查看帖子的实时版本

The HTML on the question's page shows that the GIF images for figure 6(a) and 6(b) are not responsive.问题页面上的 HTML 显示图 6(a) 和 6(b) 的 GIF 图像没有响应。

Here is the HTML for figure 6(a):这是图 6(a) 的 HTML:

<th class="css-hep4zx"><img  src="...gif" class="css-0"></th>

Note that no responsive classes are present.请注意,不存在响应类。

And here is a responsive PNG figure 3(a), with very different markup:这是一个响应式 PNG 图 3(a),标记非常不同:

<th class="css-hep4zx"><span
    class="gatsby-resp-image-wrapper"
    style="position: relative; display: block; margin-left: auto;
      margin-right: auto; max-width: 650px;">
    <span
      class="gatsby-resp-image-background-image"
      style="padding-bottom: 100%; position: relative; bottom: 0px;
      left: 0px; background-image: url(...); background-size: cover;
      display: block;"
    ></span>
    <img
      class="gatsby-resp-image-image css-0"
      src="(...).png"
      style="width: 100%; height: 100%; margin: 0px; vertical-align: middle;
      position: absolute; top: 0px; left: 0px;"
    >
  </span>
</th>

Why the PNG vs GIF Difference?为什么 PNG 与 GIF 的区别?

The very different HTML markup generated for .png and .gif images appears to be a limitation of the gatsby-remark-images plugin..png.gif图像生成的非常不同的 HTML 标记似乎是gatsby-remark-images插件的限制。 From the documentation:从文档中:

Supported Formats支持的格式

This plugin will support the following formats:该插件将支持以下格式:

• JPEG • JPEG
• PNG • PNG

Conclusion: The gatsby-remark-images plugin does not generate responsive HTML for gifs.结论: gatsby-remark-images插件不会为 gif 生成响应式 HTML。

So what now?所以现在怎么办?

So if the gatsby-remark-images plugin won't make responsive GIFs, is there a plugin that will?因此,如果gatsby-remark-images插件无法制作响应式 GIF,是否有插件可以制作?

Let's experiment with a different plugin, here's a sample gif to try:让我们尝试一个不同的插件,这里有一个示例 gif 来尝试:

在此处输入图像描述

Using the gatsby-remark-interactive-gifs , with the recommended CSS styling, produces HTML like this:使用gatsby-remark-interactive-gifs和推荐的 CSS 样式,生成 HTML 如下:

 /* CSS copied from `gatsby-remark-interactive-gifs` page */.interactive-gif {}.interactive-gif.embedded { position: relative; width: 100%; height: auto; }.interactive-gif.gif-container { position: absolute; top: 0; width: 100%; height: 100%; }.interactive-gif.gif { width: 100%; height: 100%; cursor: pointer; }
 <div class="interactive-gif"> <div class="embedded" style="padding-top: 56.28517823639775%"> <div id="nyancat.gif" class="gif-container" style="display: block;"> <img id="image-nyancat.gif" class="gif" data-original="https://i.stack.imgur.com/f7DcY.gif" src="https://i.stack.imgur.com/f7DcY.gif"> </div> </div> </div>

The resulting GIF image is indeed responsive, so it seems that using gatsby-remark-interactive-gifs does produce responsive GIFs.生成的 GIF 图像确实是响应式的,因此使用gatsby-remark-interactive-gifs似乎确实会产生响应式 GIF。

The issues come because your layout is wrapped in a <table> tag, with an inherit display: table .问题出现是因为您的布局包含在<table>标记中,并带有继承display: table It's a CSS issue so you will need to change the rules for that table, forcing the wrap of those gifs.这是一个 CSS 问题,因此您需要更改该表的规则,强制包装这些 gif。

If you are able to change your HTML structure, I would suggest a more flexible and simple structure, such as:如果您能够更改您的 HTML 结构,我会建议一个更灵活和简单的结构,例如:

<div className="your-flex-wrapper">
  <img src="gif1"/>
  <img src="gif2"/>
</div>

The wrapper should have a:包装器应该有:

.your-flex-wrapper{
   display: flex;
   flex-flow: row wrap;
}

I've applied these changes live to your site (in the th and tr tags) and:我已将这些更改实时应用到您的网站(在thtr标签中)并且:

在此处输入图像描述

However, it's not semantic to add a display: flex to a table layout so, it will be better to change the structure accordingly.但是,将display: flex添加到表格布局不是语义,因此,最好相应地更改结构。

If you force the width of the .gif to a 100% of the viewport on small screens, they will always be responsive.如果您将.gif的宽度强制为小屏幕上视口的100% ,它们将始终响应。

I was able to solve my problem with a pretty easy solution.我能够通过一个非常简单的解决方案来解决我的问题。 I've just integrated Bootstrap into my Gatsby system and then used some basic bootstrap grid classes to make the.gifs responsive.我刚刚将Bootstrap集成到我的 Gatsby 系统中,然后使用一些基本的引导网格类来使.gifs 响应。

The way I solved it:我解决它的方式:

  1. Firstly , install the bootstrap plugin-首先,安装引导插件-

    npm install react-bootstrap bootstrap

  2. Then , import the plugin where necessary (I've imported it in my post's.mdx file where I've defined those.gifs)-然后,在必要时导入插件(我已将其导入到我定义了那些.gifs 的 post's.mdx 文件中)-

    import 'bootstrap/dist/css/bootstrap.min.css'

  3. Lastly , I've just used the bootstrap way of doing things to make those.gifs responsive (that's how I defined those.gifs in my.mdx file)-最后,我刚刚使用引导程序的方式使 those.gifs 响应(这就是我在 my.mdx 文件中定义 those.gifs 的方式)-

<div class="container">
  <div class="row">
    <div class="col-sm-12 col-md-6 col-lg-6">
      <img src="./neural_net_data_manupulation_2.gif" style="width: 100%;"/>
      </div>
    <div class="col-sm-12 col-md-6 col-lg-6">
      <img src="./neural_net_decision_boundary.gif" style="width: 100%;"/>
      </div>
  </div>
</div>

That's how they look now:这就是他们现在的样子:

  • Big display (PC)-大屏(PC)-

在此处输入图像描述

  • Small screen (mobile)-小屏(手机)-

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM