简体   繁体   English

向 Shopify 添加自定义 CSS 和 JS

[英]Adding custom CSS and JS to Shopify

I am working on getting vertical tabs for a page on Shopify, using the 'Atlantic' theme.我正在使用“大西洋”主题为 Shopify 上的页面获取垂直标签。 As this theme does not have vertical tabs by default, I have used the external JS and CSS "jquery-jvert-tabs".由于这个主题默认没有垂直标签,我使用了外部 JS 和 CSS“jquery-jvert-tabs”。

My question is, if I upload the JS and CSS files as assets, how do I link them to the page on which I want to use these and how to make use of these on the page after that, if I have certain style elements available there too?我的问题是,如果我将 JS 和 CSS 文件作为资产上传,如果我有某些样式元素可用,我如何将它们链接到我想要使用这些文件的页面,以及之后如何在页面上使用这些文件还有吗?

Simply upload your filename.js file in the assets folder.只需将您的filename.js文件上传到资产文件夹中。

then drop the following in your theme.liquid head section:然后将以下内容放入您的theme.liquid头部部分:

{{ 'filename.js' | asset_url | script_tag }}

by the way, you should rename your file and add .liquid extension顺便说一句,你应该重命名你的文件并添加 .liquid 扩展名

filename.js.liquid

Good luck!祝你好运!

If I understand correctly, the asset_url filter is what you are looking for.如果我理解正确的话, asset_url 过滤器就是你要找的。

To include a JS file in a .liquid file, you use:要在 .liquid 文件中包含 JS 文件,请使用:

{{ 'script.js' | asset_url | script_tag }}

And a CSS file:和一个 CSS 文件:

{{ 'style.css' | asset_url | stylesheet_tag }}

If you don't want to pollute the global namespace you can restrict the JavaScript or CSS to certain areas.如果您不想污染全局命名空间,您可以将 JavaScript 或 CSS 限制在某些区域。

Shopify uses a simple if statement together with the page-handle for that(for www.foo.com/abcd/bar - "bar" would be the handle). Shopify 使用一个简单的 if 语句和页面句柄(对于 www.foo.com/abcd/bar - “bar”将是句柄)。

{% if page.handle == "bar" %}
    {{ 'your_custom_javascript.js' | asset_url | script_tag }}
    {{ 'your_custom_css.css' | asset_url | stylesheet_tag }}
{% endif %} 

This is extremely useful if you want to make smaller changes to certain pages.如果您想对某些页面进行较小的更改,这将非常有用。

Open theme.liquid and check how the css and js are included and add yours by following them.Add your custom file to assets.打开 theme.liquid 并检查 css 和 js 是如何包含的,然后按照它们添加您的。将您的自定义文件添加到资产中。 Css And Js Add Image attached Css 和 Js 添加图片附加

For JS files, never create the file directly via Shopifys "Create a blank file"-dialogue.对于 JS 文件,切勿直接通过 Shopifys 的“创建空白文件”对话创建文件。 Instead, create the file locally as .js (not .js.liquid), then upload to the assets folder.相反,在本地将文件创建为 .js(不是 .js.liquid),然后上传到资产文件夹。

You can then simply reference the file in the theme.liquid head section:然后,您可以简单地引用 theme.liquid 头部部分中的文件:

{{ 'filename.js' | asset_url | script_tag }}

Background:背景:

It seems that Shopify always sets the mime type to text/x-liquid when creating new files and if liquid is used for the theme, regardless of the file extension.似乎 Shopify 在创建新文件时总是将 mime 类型设置为 text/x-liquid 并且如果将液体用于主题,无论文件扩展名如何。 This will then result in a browser error like this one from Chrome:然后,这将导致 Chrome 出现类似这样的浏览器错误:

Refused to apply style from ... because its MIME type ('text/x-liquid') is not a supported stylesheet MIME type, and strict MIME checking is enabled拒绝从 ... 应用样式,因为它的 MIME 类型('text/x-liquid')不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查

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

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