Quantcast
Channel: CSSタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8679

【Rails6+Bootstrap/1000%時短】画像をhoverすると自動ドアのようにスーッと投稿内容が現れるCSSアニメーション実装

$
0
0

rails_css_hover_animation_2.png

環境

ruby 2.6.4

Rails 6.0.2.2

rbenv 1.1.2

bootstrap 4.3.1

Bootstrap4の導入

①bootstrap 4.3.1を追加するためにyarnをinstall

$ yarn add bootstrap@4.3.1 jquery popper.js

②app/javascript/packs/application.jsに下記コードを追加

app/javascript/packs/application.js
import"bootstrap"

③config/webpack/environment.jsを下記のように変更

config/webpack/environment.js
const{environment}=require('@rails/webpacker')constwebpack=require("webpack")environment.plugins.append("Provide",newwebpack.ProvidePlugin({$:'jquery',jQuery:'jquery',Popper:['popper.js','default']}))module.exports=environment

④app/assets/stylesheets/application.cssを下記のように変更

app/assets/stylesheets/application.css
/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
 * vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require bootstrap 
 *= require_tree .
 *= require_self
 */

⑤custom.css.scssをapp/assets/stylesheets以下に作成して下記コードを追加

app/assets/stylesheets/custom.css.scss
@import'bootstrap/dist/css/bootstrap';

それではカスタマイズしていきましょう!

画像から自動ドアのようにスーッと投稿内容が現れるデザイン実装

あらかじめRailsで作成しておいた投稿アプリにデザインを入れていきます

コピペだけでできちゃうのでパパッとクールなデザインにしちゃいましょー!

今回は画像をhoverすると自動ドアのようにスーッと投稿内容が現れるCSSアニメーション実装をしていきます
rails_css_hover_animation_1.png


上の画像がデフォルトの状態です

画像をhoverすると下の画像のように背景と文章が現れるアニメーションとなるようカスタマイズしていきます
rails_css_hover_animation_2.png

rails_css_hover_animation_3.png


では早速コードをコピペしていきましょう

app/views/articles/index.html.erb

app/views/articles/index.html.erb
<divalign="center"><%= will_paginate %>
</div>
<% @article.each do |article| %>
  <% @num +=1%><divclass="row"><divclass="col-xs-8 col-xs-offset-2"><divclass="well well-lg"><divclass="pic"style="<%= "background-image:url('https://source.unsplash.com/1000x80" + "#{@num}" %>" fluid alt="Fluid image');">
          <div class="screen-left"></div>
          <div class="screen-right"></div>
          <div class="fontscenter">
            <h1>
              <%= link_to article.title, article_path(article) %>
            </h1>
            <p>
              <%= truncate(article.description, length: 100) %>
              <p>
                <%# user and article are associated in models and you can write "article.user_id" to like "article.user" on rails below the code %>
                <small>Created by: <%= article.user.username if article.user %>,
                      <%= time_ago_in_words(article.created_at) %> ago,
                      last updated: <%= time_ago_in_words(article.updated_at) %> ago</small>
              </p>
            </p>
          </div>
        </div>
        <% if article.categories.any? %>
          <p>Categories: <%= render article.categories %></p>
        <% end %>
        <% if logged_in? && (current_user == article.user || current_user.admin?) %>
          <div class="article-actions">
            <%= link_to "Editthisarticle", edit_article_path(article), class: "btnbtn-xsbtn-primary" %>
            <%= link_to "Deletethisarticle", article_path(article), method: :delete,
                                                data: {confirm: "Areyousureyouwanttodeletethearticle?"},
                                                class: "btnbtn-xsbtn-danger" %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
<% end %>
<div align="center">
<%= will_paginate %>
</div>

app/assets/stylesheets/custom.css.scss

app/assets/stylesheets/custom.css.scss
.container{width:960px;margin:0auto;}.container:after{clear:both;display:table;content:'';}.bg-pic{width:100%;height:500px;margin:20px;background-color:white;float:left;cursor:pointer;box-shadow:3px3px5px0pxrgba(0,0,0,0.5);}.pic{width:100%;height:500px;position:relative;overflow:hidden;background-color:#102B46;}.fonts{background-color:rgba(0,0,0,0.5);width:100%;height:500px;padding:10px;top:0;left:0;font-family:georgia;color:#fff;opacity:0;transition:opacity.8s;}.fontsh1{margin-top:100px;margin-bottom:40px;}.fontsp{font-size:14px;font-style:italic;text-align:center;line-height:20px;}.pic:hover.fonts{opacity:1;transition:opacity.2s.3s;}.picdiv{position:absolute;}/* screen open and close */.screen-right,.screen-left{width:50%;height:inherit;background-color:#000;top:0;transition:all.3s;}.screen-right{left:100%;}.screen-left{right:100%;}.pic:hover.screen-right{transition:all.3s;left:50%;}.pic:hover.screen-left{transition:all.3s;right:50%;}

app/controllers/articles_controller.rb

app/controllers/articles_controller.rb
defindex@articles=Article.paginate(page: params[:page],per_page: 5)@num=0end

いかがでしたでしょうか?

自動ドアのようにスーッとなる動きは出ましたか?

こういうの実装できるとめっちゃ興奮しますよね

バックエンドのみでも楽しいですが、フロントエンドは見た目に出る分かなり感覚が刺激されます

今までに作ったポートフォリオとかにアニメーションをつけるのとかもいいですねー

他の魅力的なアニメーションの作成方法もたくさん掲載しているので是非挑戦してみてください

スクリーンショット-2020-06-02-17.23.10.png


Viewing all articles
Browse latest Browse all 8679


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>