English 中文(简体)
案文适合剩余空间并保持同一动议的效力
原标题:Make text fit the remaining space and keep the same motion effect

我在以下结构中有一个下游项目:

.
├── media
│   └── img.jpg
├── package.json
├── package-lock.json
├── 页: 1
├── src
│   ├── components
│   │   └── AnimatedCharacters.js
│   ├── pages
│   │   ├── _app.js
│   │   ├── index.js
│   │   └── index_.js
│   └── styles
│       └── global.css
└── tailwind.config.js

The content of the files is as follows:
package.json

{
  "private": true,
  "scripts": {
    "build": "next build",
    "dev": "next dev",
    "start": "next start"
  },
  "dependencies": {
    "eslint": "^8.54.0",
    "framer-motion": "^10.16.5",
    "next": "latest",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },
  "engines": {
    "node": ">=18"
  },
  "devDependencies": {
    "autoprefixer": "^10.4.16",
    "postcss": "^8.4.31",
    "tailwindcss": "^3.3.5"
  }
}

页: 1

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

tailwind.config.js

/** @type {import( tailwindcss ).Config} */

const { fontfamily } = require("tailwindcss/defaultTheme");

module.exports = {
  content: ["./src/**/*.{js,ts,jsx,tsx,mdx}"],
  theme: {
    extend: {
      fontFamily: {
        poppins: ["var(--font-poppins)"],
      },
      colors: {
        dark: "#1b1b1b",
        light: "#f5f5f5",
        primary: "#B63E96", // 240,86,199
        primaryDark: "#58E6D9", // 80,230,217
        white: "#fff",
        light_purple: "#8490ff",
        light_white: "#f9f9ff",
        text_color: "#777777",
      },
    },
    borderWidth: {
      1: "1px",
      2: "2px",
    },
  },
  plugins: [],
};

弧/型/全球

@tailwind base;
@tailwind components;
@tailwind utilities;

src/pages/_app.js

import Head from "next/head";
import "../styles/global.css";
import { Poppins } from "next/font/google";

const poppins = Poppins({
  subsets: ["latin"],
  variable: "--font-poppins",
  weight: ["100", "200", "400", "300", "500", "600", "700"],
});

export default function App({ Component, pageProps }) {
  return (
    <>
      <Head>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <main className={`${poppins.variable} font-poppins`}>
        <Component {...pageProps} />
      </main>
    </>
  );
}

src/pages/index.js

import Head from "next/head";
import Image from "next/image";
import profilepicture from "../../media/img.jpg";

export default function Home() {
  return (
    <div>
      <Head>
        <title>Create Next App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <main className="w-full h-auto pl-14 pr-14 flex bg-light_white text-black">
        <div className="flex items-center justify-between w-full pt-20 pb-20">
          <div className="flex-1 pr-14">
            <p className="text-text_color mb-2">
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
              eiusmod tempor incididunt ut labore et dolore magna aliqua.
            </p>
            <p className="text-text_color mb-2">
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
              eiusmod tempor incididunt ut labore et dolore magna aliqua.
            </p>
            <p className="text-text_color mb-2">
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
              eiusmod tempor incididunt ut labore et dolore magna aliqua.
            </p>
          </div>
          <div className="w-80 h-80">
            <Image
              src={profilepicture}
              alt="bird"
              className="w-full h-full object-cover rounded-full"
            />
          </div>
        </div>
      </main>
    </div>
  );
}

When running npm run dev, this works as expected; the text takes the space left after the image is well placed.

“在此处的影像描述”/</a

此外,当我转播窗口时,案文也适应空间。

“entergraph

然而,当我添加<条码>AnimatedCharacters.js部分时,案文的利害关系与空间的需要一样大,在我转播时,空间甚至超过了屏幕的宽度。

Here the code:
src/components/AnimatedCharacters.js

import React from "react";
import { motion } from "framer-motion";

export default function AnimatedCharacters({
  text,
  className = "",
  reverse = false,
}) {
  // splitting text into letters
  const letters = Array.from(text);

  // Variants for Container
  const container = {
    hidden: { opacity: 0 },
    visible: (i = 1) => ({
      opacity: 1,
      transition: {
        staggerChildren: 0.02,
        delayChildren: 0.03 * i,
        staggerDirection: reverse ? -1 : 1, // Adjust staggerDirection based on reverse prop
      },
    }),
  };

  // Variants for each letter
  const child = {
    visible: {
      opacity: 1,
      x: 0,
      y: 0,
      transition: {
        type: "spring",
        damping: 12,
        stiffness: 100,
      },
    },
    hidden: {
      opacity: 0,
      x: reverse ? 20 : -20, // Adjust initial x position based on reverse prop
      y: 10,
      transition: {
        type: "spring",
        damping: 12,
        stiffness: 100,
      },
    },
  };

  return (
    <motion.div
      className={className}
      variants={container}
      initial="hidden"
      animate="visible"
    >
      {letters.map((letter, index) => (
        <motion.span variants={child} key={index}>
          {letter === " " ? "u00A0" : letter}
        </motion.span>
      ))}
    </motion.div>
  );
}

And this is how index.js looks like after adding the new component:

import Head from "next/head";
import Image from "next/image";
import profilepicture from "../../media/img.jpg";
import AnimatedCharacters from "../components/AnimatedCharacters";

export default function Home() {
  return (
    <div>
      <Head>
        <title>Create Next App</title>
      </Head>
      <main className="w-full h-auto pl-14 pr-14 flex bg-light_white text-black">
        <div className="flex items-center justify-between w-full pt-20 pb-20">
          <div className="pr-14">
            <AnimatedCharacters
              text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
              do eiusmod tempor incididunt ut labore et dolore magna aliqua."
              className="text-text_color mb-2 overflow-hidden flex"
            />
            <AnimatedCharacters
              text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
              do eiusmod tempor incididunt ut labore et dolore magna aliqua."
              reverse
              className="text-text_color mb-2 overflow-hidden flex"
            />
            <AnimatedCharacters
              text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
              do eiusmod tempor incididunt ut labore et dolore magna aliqua."
              className="text-text_color mb-2 overflow-hidden flex"
            />
          </div>
          <div className="w-80 h-80">
            <Image
              src={profilepicture}
              alt="bird"
              className="w-full h-full object-cover rounded-full"
            />
          </div>
        </div>
      </main>
    </div>
  );
}

Here is a link to a video record I just made.

是否有办法使案文适合图像形成后留下的任何空间,并按行文对格式案文行文(即从左边看,N+1线从右边看)适用该动议的效力?

I am using Tailwind CSS and framer-motion

问题回答

if u managed to make the text wrap inside the div

there is a problem in this line {letter === " " ? "u00A0" : letter} because now every letter is in a span so lets say the word text if you resized the page it might not be displayed text it might be like:

te
xt

so before you splitting text into letters const letters = Array.from(text);
your gonna have to separate every word in a span

code:

const text="separate- every- word- with- any- symbol- also- with- a- white- space"

return (
       <div>
      {text
        .replaceAll("-", `${"u00A0"}`)
        .split(" ")
        .map((word, i) => (
          <div key={"word" + i}>
            {word.split("").map((letter, i) => (
              <motion.span key={"letter" + i}>{letter}</motion.span>
            ))}
          </div>
        ))}
    </div>
)
  1. what i did is the text i replaced the symbol with a space
  2. i used split and the value is the space " " to separate every word
  3. i used split again to separate every letter in evey word and put it in motion.span




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签