【问题标题】:Line Animation while using jQuery使用 jQuery 时的线条动画
【发布时间】:2019-02-18 18:46:29
【问题描述】:

我的客户要求将文本(基本 p 标签)与图像中的按钮连接起来(请参阅附图),我很难找出如何完成此操作并确保它具有响应性。

我对如何实现这一点有两个想法:

导入线条图像(客户给了我一个 png 文件)并使用 CSS 使线条正确适合图像。问题是一旦我缩小屏幕尺寸,它就会失去正确的位置。我在codepen上有我的代码,除了那些乱线之外,我可以从上面的图像中得到所有的东西: https://codepen.io/dec23rd1986/full/PdEoZa/

我也考虑过 SVG 动画,但我不确定如何将它与我用于按钮的 jQuery 结合起来。我用它在两个按钮和侧箭头按钮之间来回切换,以及淡入淡出效果。

如果有人能指出如何完成此任务的正确方向(文章、视频、最佳方法),我将不胜感激!
最好的问候,

   <div class="container-fluid jumbo_features">
    <div class="row">
     <div class="col-md-6">
      <img src="https://image.ibb.co/ijvTfU/feature_meeting.png" class="meeting_img">

      <img src="https://image.ibb.co/hZNxPp/feature_tasks.png" class="tasks_img"></div>

     <div class="col-md-6">
      <button type="button"  class="btn active" aria-pressed="true" id="meeting_button">Meeting</button>
      <button type="button" class="btn" aria-pressed="true" id="tasks_button">Tasks</button>

       <br>
     <p class="meeting_info"><b>Attends Meetings:</b><br>Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p><br>
     <p class="meeting_info "><b>Takes meeting notes:</b><br>Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p>
      <!--Tasks-->
       <p class="tasks_info"><b>Example One</b><br>Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p><br>
              <p class="tasks_info"><b>Example Two</b><br>Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p><br>
              <p class="tasks_info"><b>Example Three</b><br>Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p><br>

         </div>
          </div>
       </div>

      <br>
       </div>
      </div>  
    </div>

</div>
</div>
       <div class ="meeting_info">
<img src="https://thumb.ibb.co/c4yUFU/line1.png" id="line">

【问题讨论】:

  • 我个人会使用 SVG。你试过使用它吗?您可以通过使用所有值的百分比而不是像素位置来缩放线条。
  • 嗨,鲍勃!我最近一直在研究 SVG,但从未使用过它。您是否建议我通过 SVG 创建线条或使用保存为 SVG 文件的图像,然后使用 CSS 设置样式?我在我的 codepen 中对此进行了测试,但在如何使用我的 jQuery 合并它时遇到了麻烦。你能告诉我 SVG 中的这个功能叫什么吗?
  • 我只是制作线条。我会在答案中举一个例子。你也可以玩this

标签: javascript jquery css animation svg


【解决方案1】:

虽然我不清楚为什么您的 jQuery 可能会干扰您对线条的使用,但这里有一个简单的 SVG 示例,它会随着您更改窗口大小而缩放。要使其缩放,您所要做的就是使用百分比值而不是文字像素值。

如果您在屏幕尺寸变化时有一些比例变化,那么您可能需要以编程方式更改此代码。如果您提供更多关于您为何担心与 jQuery 交互的信息,请分享它们,我会更新我的答案。

编辑:我更新了我的示例,以大致了解我认为您正在寻找的内容(例如,消息并不总是正确排列)。如果我理解你的话,你可以从那里改进它。此外,您可能希望使用不同尺寸的屏幕更改百分比。如果是这样,请在 CSS 中使用 @media 查询。

div {
  display: inline-block;
  vertical-align: top;
  width: 32%;
}

#myImg img {
  width: 100%;
}

#mySVG {
  margin-top: 20%;
  margin-left: -10%;
}

#myMsg {
  margin-top: 28.5%;
  padding-left: 10px;
}
<div id="myImg">
  <img src="https://www.yotako.io/images/documentation/balsamiq/containers/android_balsamiq.png" />
</div>
<div id="mySVG">
  <svg height="100%" width="100%">
  <line x1="0" y1="0" x2="50%" y2="0" style="stroke:#000;stroke-width:2" />
  <line x1="50%" y1="0" x2="50%" y2="50%" style="stroke:#000;stroke-width:2" />
  <line x1="50%" y1="50%" x2="100%" y2="50%" style="stroke:#000;stroke-width:2" />
</svg>
</div>
<div id="myMsg">
  <p>Your message goes here</p>
</div>

【讨论】:

  • 非常感谢鲍勃。问题,我如何使这个 SVG 与左侧图像按钮完美对齐?这是在 CSS 中修复边距的问题吗?
  • @Alexandra 是的,也不是。我会将 SVG 放在一个 div 中,并使用 CSS 来调整 div 的大小和对齐(是的,可能使用边距而不是填充,因为您似乎希望线条在某种程度上与图像重叠),因为您希望它与你的手机图片。
  • @Alexandra 我在回答中添加了一个粗略的例子。
  • @Alexandra 不客气。希望一切都对你有用。
  • @Alexandra 好的,我快速浏览了一下。我不知道引导程序,但是为什么您在引导程序容器中也没有 SVG? (我认为这是 container-fluid 类的主要 div。)换句话说,你为什么不采用我这里的 div 布局并将整个内容包含在引导容器中?
【解决方案2】:

线条动画部分,除了svg自带的动画功能,你不需要使用其他任何东西:

<svg height="120" width="120">
  <polyLine
    id="myLine"
    points="120,0 60,0 60,60 0,60"
    fill="none"
    stroke-width="2"
    stroke="red"
    stroke-dasharray="240"
    stroke-dashoffset="240"
  />

  <animate
    xlink:href="#myLine"
    attributeName="stroke-dashoffset"
    from="240"
    to="0"
    dur="3s"
    begin="1s"
    fill="freeze"
   />   
</svg>

PS:你可以得到总长度(240with calculation,但是对于有限的行数可以手动计算。

【讨论】:

  • 谢谢你!动画部分对我的客户来说是一个重要的功能。我会去看看这个。
  • points 表示x1,y1 x2,y2 x3,y3 ... 格式的坐标。我更改了示例中的点。
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2015-03-17
  • 2013-12-21
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多